[leetcode] 3. Longest Substring Without Repeating Characters

原创 2015年11月18日 20:20:46

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

这道题找字符串中不重复字符的最长子串长度,难度为Medium。

  • 首先想到的是用HashTable。从字符串头部开始计算子串,将没有重复出现的字符存入HashTable中,如果遇到相同的字符,表明当前不重复子串已经结束,从HashTable中查到的重复字符位置开始重新计算子串,比较确定此子串是否是当前最长的,遍历整个字符串后得到不重复字符的最长子串长度。具体代码:
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            unordered_map<char, int> hash;
            int maxLen = 0;
            int pos = -1;
            
            for(int i=0; i<s.size(); i++) {
                auto it = hash.find(s[i]);
                if((it != hash.end()) && (it->second >= pos)) {
                    pos = it->second;
                }
                hash[s[i]] = i;
                maxLen = max(maxLen, i-pos);
            }
            
            return maxLen;
        }
    };
  • 通过之后查看了别人的代码,发现有不用HashTable的方法,非常巧妙,用字符ascii码做下标把字符位置存入vector中,避免使用HashTable,效率上有所提高,复制其代码供大家参考:
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            vector<int> dict(256, -1);
            int maxLen = 0, start = -1;
            for (int i = 0; i != s.length(); i++) {
                if (dict[s[i]] > start)
                    start = dict[s[i]];
                dict[s[i]] = i;
                maxLen = max(maxLen, i - start);
            }
            return maxLen;
        }
    };
版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode:3. Longest Substring Without Repeating Characters (java)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: Given a str...
  • j754379117
  • j754379117
  • 2016年06月20日 20:36
  • 857

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUnique...
  • feliciafay
  • feliciafay
  • 2013年11月23日 04:25
  • 16847

LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/] Given a string, ...
  • dragon_dream
  • dragon_dream
  • 2014年02月18日 22:22
  • 4657

Longest Substring Without Repeating Characters -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/  这道题用的方法是在Leet...
  • linhuanmars
  • linhuanmars
  • 2014年02月26日 01:53
  • 18844

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015年02月27日 12:55
  • 5791

LeetCode 3.Longest Substring Without Repeating Characters c语言版

问题描述: Given a string, find the length of the longest substring without repeating characters. Exa...
  • banana1006034246
  • banana1006034246
  • 2017年07月15日 17:35
  • 309

leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, ...
  • wangyaninglm
  • wangyaninglm
  • 2016年04月05日 22:27
  • 2732

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For examp...
  • hu1020935219
  • hu1020935219
  • 2014年08月03日 20:38
  • 7649

leetcode python - Longest Substring Without Repeating Characters

# Given a string, find the length of the longest substring without repeating characters. # # Example...
  • AMDS123
  • AMDS123
  • 2017年04月01日 15:07
  • 6338

Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode

Method 1 (Simple) We can consider all substrings one by one and check for each substring whether it ...
  • hellobinfeng
  • hellobinfeng
  • 2014年01月05日 01:56
  • 3311
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 3. Longest Substring Without Repeating Characters
举报原因:
原因补充:

(最多只允许输入30个字)