LeetCode--无重复字符的最长子串
2019-04-13 20:05
260 查看
题目:
答案(查看到最好的):
class Solution { public: int lengthOfLongestSubstring(string s) { int size,i=0,j,k,max=0; size = s.size(); for(j = 0;j<size;j++){ for(k = i;k<j;k++) if(s[k]==s[j]){ i = k+1; break; } if(j-i+1 > max) max = j-i+1; } return max; } };
结果:
采用滑动窗口的方法,由前往后逐渐扩展,每次搜索有无重复均是往前搜索。
基本思路:
1、记左侧判断点为A[i],且起始判断点为A[0],起始当前元素为A[0]
2、判断当前元素A[j]之前是否已经存在该元素
3、
(1)若不存在,将长度更新为:j-i+1;并继续更新当前元素为下一元素,即j=j+1,跳转至步骤2
(2)若存在,则将左侧判断点更新为:重复元素的下标+1,将长度更新为:j-i+1
4、输出长度
相关文章推荐
- leetcode 395. 至少有K个重复字符的最长子串
- LeetCode——3无重复字符的最长子串
- [leetcode]无重复字符的最长子串
- LEETCODE-3 无重复字符的最长子串
- 用python写leetcode【13】 -- 至少有K个重复字符的最长子串(395)
- leetcode第三题:无重复字符的最长子串
- leetcode3无重复字符的最长子串
- 【Leetcode】3. 无重复字符的最长子串[未解出]
- LeetCode[3] 无重复字符的最长子串
- LeetCode题目C++实现:3. 无重复字符的最长子串
- LeetCode3. 无重复字符的最长子串(python)
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- leetcode 3:无重复字符的最长子串
- LeetCode题解(python)-3.无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- LeetCode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
- leetcode(3)无重复字符的最长子串
- LeetCode 3 无重复字符的最长子串(哈希表或数组)
- Leetcode刷题35-3.无重复字符的最长子串(C++)