LeetCode 第三题,Longest Substring Without Repeating Characters

原创 2014年08月03日 20:38:44

题目:

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.

题意:

就是寻找一个给定字符串中的不重复最长字串的长度。

比如abcdab,最长的串位abcd或cdab,长度位4.

解法:

第一种方法是暴搜,不过显然会超时,所以没有尝试。

第二种方法是暴搜的改良版:

代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
		int index = 0;
		int max = 0;
		int len = s.length();
		if( len == 0)
		return 0;
		if (len ==1)
		return 1;
		for (int i =1; i< len; i++)
		{
			for(int j=i-1; j>=index; j--)
			{
				if(s[i] == s[j])
				{
					index = j+1;
					break;
				}
				else
				{
					if(max < i-j+1)
					max = i - j +1;
				}
			}
		}
		return max;   
    }
};
即是寻找当前下标到字串start是否有重复字符,重复则将start置为重复下标加一。

第三种方法显然是hash表,用一个hash table保存每个字符上一次出现过的位置。从前往后扫描,假如发现字符上次出现过,就把当前子串的起始位置start移动到上次出现过的位置之后——这是为了保证从start到i的当前子串中没有任何重复字符。同时,由于start移动,当前子串的内容改变,start移动过程中经历的字符都要剔除。

代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int start = 0; // current start point of substring without dup
        int maxlen = 0; // max length of substring found
        int table[256]; // hash table for index of each char appeared
        for (int i = 0;i < 256;i++) table[i] = -1; // if char not present, index is -1
        int len = s.length();
        for (int i = 0;i < len;i++) {
            if (table[s[i]] != -1) {
                while (start <= table[s[i]]) table[s[start++]] = -1;
            }
            if (i - start + 1 > maxlen) maxlen = i - start + 1;
            table[s[i]] = i;
        }
        return maxlen;
    }
};

而第二种方法和第三种方法时间差距如下:


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:27
  • 3761

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...
  • likecool21
  • likecool21
  • 2013-09-01 22:48:12
  • 15794

LeetCode(3) Longest Substring Without Repeating Characters

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

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:19
  • 6486

Longest Substring Without Repeating Characters 字符串中最长的无重复子串长度

Longest Substring Without Repeating Characters
  • yujin753
  • yujin753
  • 2014-12-04 20:49:30
  • 1238

关于leetcode第三题的巧妙解法

最近在leetcode上刷题,无聊看到第三题,题干如下:   Longest Substring Without Repeating Characters:   Given a string, fi...
  • ysayk
  • ysayk
  • 2016-05-26 11:38:32
  • 2091

LeetCode第3题

3. Longest Substring Without Repeating Characters Given a string, find the length of the longest su...
  • q874724218
  • q874724218
  • 2016-07-03 23:55:57
  • 485

leetcode小白解题记录——第三题

3. Longest Substring Without Repeating Characters Given a string, find the length of the long...
  • lin_tuer
  • lin_tuer
  • 2016-08-18 16:14:18
  • 674

leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)

leetcode第三题,自己的算法,不知道复杂度怎样,希望能与网友交流。
  • hellowangxyue
  • hellowangxyue
  • 2016-04-05 09:47:32
  • 636

Longest Substring Without Repeating Characters

对leetcode第三题Longest Substring Without Repeating Characters的解法分析标签(空格分隔): 编程 程序参考了http://blog.sina....
  • dongchangzhang
  • dongchangzhang
  • 2016-09-07 23:58:13
  • 316
收藏助手
不良信息举报
您举报文章:LeetCode 第三题,Longest Substring Without Repeating Characters
举报原因:
原因补充:

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