`
jgsj
  • 浏览: 956864 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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.

这道题目也是下标难以处理,关键点:

1 利用maxlength变量记录当前已经查找到的最长不重复子串

2 巧妙的移动两个下标,如下图示:

class Solution {
public:
	int lengthOfLongestSubstring(string s) {
		int n = s.length();
		int i = 0;
		int j = 0;
		bool found[26] = {false};
		int maxLength = 0;
		for (; i < n; i++)
		{
			if (!found[s[i] - 'a'])
			{
				found[s[i] - 'a'] = true;
			}
			else
			{
				maxLength = max(maxLength, i-j);
				//注意:这里是关键,i是等到第二次s[i]==s[j]的时候才往前进的,而且是始终指向前一次重复出现字母的后一个字母。
				while (s[i] != s[j])
				{
					found[s[j] - 'a'] = false;
					j++;
				}
				j++;
			}
		}
		maxLength = max(maxLength, n-j);
		return maxLength;
	}
};


分享到:
评论

相关推荐

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

    答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...

    javalruleetcode-Leetcode:力码解决方案

    (无重复字符的最长子串) Medium Dual Pointer (Sliding Window), Hash Table 4 Median of Two Sorted Arrays (寻找两个有序数组的中位数) Hard Divide and Conquer 5 Longest Palindromic Substring (最长回文子串) ...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    无重复字符的最长子串 string 4 LMedian of Two Sorted Arrays 两个排序数组的中位数 ary,binary search,dive and conquer 5 Longest Palindromic Substring 最长回文子串 string,dp 8 String to Integer(atoi) 字符...

    lrucacheleetcode-leetcode-1:leetcode-1

    最长没有重复字符的子序列 记录各字符最近一次出现的位置 4. Median of Two Sorted Arrays 求两有序数列的中位数,可泛化为求两有序数列的第k位数,二分法 5. Longest Palindromic Substring 最长回文子串,补符号,...

    leetcode #3 无重复字符的最长子串(C)

    链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters    一上来没啥思路,好久不做题了,脑袋都不大好使了;瞎想了半天,有个差不多的思路,在纸上画的明明白白,但是一写...

    leetcode跳跃-LeCode:乐科

    无重复字符的最长子串 4. Median of Two Sorted Arrays 寻找两个有序数组的中位数 5. Longest Palindromic Substring 最长回文子串 6. ZigZag Conversion Z字型变换 7. Reverse Integer 整数反转 8. String to ...

    leetcode跳跃-leetcode:leetcode一天一次

    无重复字符的最长子串:3. Longest Substring Without Repeating Characters - 最小跳跃步数问题 - 动态规划 + 循环策略优化:45. Jump Game II - 二叉树 前序遍历判断二叉树:98. Validate Binary Search Tree - 二...

    leetcode题库-LeetCode:力码

    无重复字符的最长字串 Longest Substring Without Repeating Characters.cpp 4 寻找两个有序数组的中位数 Median of Two Sorted Arrays.cpp 5 最长回文子串 Longest Palindromic Substring.cpp 7 整数反转 Reverse ...

    leetcode分类-Leetcode:leetcode

    最长的不出现重复字符的子串。 典型的two points问题,枚举左端点,维护右端点的右移。用个数组记录一下字符出现个数就ok了。O(n)。 不了解two points的话,也很容易想到具有单调性,枚举左端点,二分右端点,维护...

    leetcode数组下标大于间距-LeetCode:力扣GoLang

    longest-substring-without-repeating-characters: 最长不重复子串 最长不重复子串(获取长度) 使用一个可截取的字符串,或者队列(FIFO)来遍历一遍原字符串,当有字母重复的时候,开始从头删,一直到删除重复的元素,然后...

    acm和leetcode难度-leetcode:leetcode算法分析和代码实现

    acm和leetcode难度 leetcode 解题列表 俗话说: 熟读唐诗三百首, ...没有重复字母的最长子串 [Longest Substring Without Repeating Characters] ★★☆☆☆ 字符串 / 判重 4 两个有序数组的中值 [Median of Two So

    leetcode答案-SH_LeetCode:LeetCode-Swift

    leetcode 答案 SH_LeetCode LeetCode-Swift解答记录(Answer records of LeetCode) 11月21日 更新:添加README文件以及LeetCode前三题代码...无重复字符的最长子串(Longest Substring Without Repeating Characters)

    leetcode数组下标大于间距-LeetCode_Solutions::party_popper:我的力扣解决方案

    结尾的子串,内层循环根据st[i]检查是否有重复。这种方法可以将时间复杂度从 O(n^3) 降低到 O(n^2)。 0004. Median of Two Sorted Arrays 解题思路 二分查找,并且使用了Median的性质。对于A的划分i,表示以第i - 1...

    无重复字符的最大子串

    链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 思路 构造一个辅助字符串 sub ,运用字符串的 find 函数判断当前字符是否存在于辅助字符串 sub 中,如果不存在则添加进去...

    leetcode双人赛-Strings_Algorithms_C-:用C++编写的字符串算法

    :包含问题的解决方案:给定一个字符串,找到没有重复字符的最长子串的长度。 ****************************************************** ********** is_anagram_O(n).cpp :包含O(n) 时间复杂度问题的解决方案:给定...

    leetcode下载-lengthOfLongestSubstring:使用webpack打包一个公共基础包,并且把包发到npm

    https://leetcode-cn.com/problems/longest-substring-without-repeating-characters 下面开始一步步介绍: 新建项目目录:mkdir lengthOfLongestSubstring 进入目录:cd lengthOfLongestSubstring 初始化

    leetcode2sumc-leetcode:leetcode

    [python](./Longest_Substring_Without_Repeating_Characters.py) 中等的 4 [两个有序数组的中位数]() [python](./Median_of_Two_Sorted_Arrays.py) 难的 5 [最长回文子串]() [python](./Longest_Palindr

    leetcode296-Algorithm:来自https://github.com/xidui/algorithm-training的算法演

    [无重复字符的最长子串](#Longest-Substring-Without Repeating-Characters) [ZigZag 转换](#ZigZag 转换) [反向整数](#反向整数) [回文数](#回文数) 二和 给定一个整数数组,找到两个数字,使它们相加为特定的目标...

Global site tag (gtag.js) - Google Analytics