LeetCode 3_Longest Substring Without Repeating Characters

题目描述:

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.

也就是寻找字符串中的不含重复元素的最长子串的长度。

首先很容易想到的是暴力法:外两层循环在字符串中不断的扫描子串,内两层循环用来判断子串是否是有重复字符。显然这种方法时间复杂度有点高为O(n^4),基本上不可以被接受。

在上面的方法中,判断子串是否有重复的过程中可以不用用两层循环来扫描,可以使用哈希表的方法判断。代码如下:

<span style="font-size:18px;">class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
        int i,j;
        int maxLength = 0;
        int hash[256];
        int n = s.size();
        for(i = 0; i < n; ++i)
        {
            memset(hash,0,sizeof(hash));
            hash[s[i]] = 1;
            for(j=i+1; j<n; ++j)
            {
                if(hash[s[j]] == 0)
                    hash[s[j]] = 1;
                else
                    break;
            }
            if(j-i > maxLength)
                maxLength = j-i;
        }
        return maxLength;
    }
};</span>

上面的方法时间复杂度为O(n^2),下面给出LeetCode的参考答案,时间复杂的为O(n),而且代码非常简单!真实不得不佩服大牛。。。

思路:使用i和j两个指针进行搜索,i代表候选的最长子串的开头,j代表候选的最长子串的结尾。先假设i不动,右移j,直到j到达原字符串的结尾,此时j-i就构成了一个候选的最长子串。每次都维护一max_length,就可以选出最长子串了。如果字符j已经重复出现过(假设在位置k),就需要停止右移了。记录当前的候选子串并和max_length做比较。下面就是一个很好的处理,还真没想到!

在下一次搜寻中,i应该更新到k+1。这句话的意思是,用这个例子来理解,abcdef是个不重复的子串,abcdefc中(为了方便记录为abc1defc2),c1和c2重复了。那么下一次搜寻,应该跨过出现重复的地方进行,否则找出来的候选串依然有重复字符,且长度还不如上次的搜索。所以下一次搜索,直接从c1的下一个字符d开始进行,也就是说,下一次搜寻中,i应该更新到k+1。

代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
	int i = 0, j = 0;
	int maxLength = 0;
	bool exist[256] = {false};
	int n = s.length();
	while (j < n)
	{
		if (exist[s[j]])<span style="white-space:pre">	// 因为s[j] 中的是字符,可以自动转换为整型</span>
		{
			maxLength = max(maxLength, j - i);
			while(s[i] != s[j])
			{
				exist[s[i]] = false;
				i++;
			}
			i++;
			j++;
		}
		else
		{
		<span style="white-space:pre">	</span>exist[s[j]] = true;
			j++;
		}
	}
	maxLength = max(maxLength, n-i);<span style="white-space:pre">	// 别忘了这一步</span>
	return maxLength;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 07:12:31

LeetCode 3_Longest Substring Without Repeating Characters的相关文章

LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

题目: 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 s

LeetCode: Longest Substring Without Repeating Characters 题解

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 subst

LeetCode——Longest Substring Without Repeating Characters

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 subst

leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

题目链接 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

[LeetCode] Longest Substring Without Repeating Characters [15]

题目 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 su

[LeetCode] Longest substring without repeating characters 最长无重复子串

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 subst

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减