[LeetCode] Longest Substring Without Repeating Characters 解题报告
来源:互联网 发布:智能电视点歌软件 编辑:程序博客网 时间:2024/06/11 00:01
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.
» Solve this problem
[解题思路]
从左往右扫描,当遇到重复字母时,以上一个重复字母的index +1,作为新的搜索起始位置。比如
直到扫描到最后一个字母。
[Code]
Version 1
1: int lengthOfLongestSubstring(string s) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: int count[26];
5: if(s.size() ==0) return 0;
6: memset(count,-1, sizeof(count));
7: int start = 0;
8: int maxV = 0;
9: for(int i=0; i< s.size(); i++)
10: {
11: int index = s[i] - 97;
12: if(count[index] >= 0)
13: {
14: if(maxV < (i -start))
15: {
16: maxV = i-start;
17: }
18: i = count[index];
19: start = i+1;
20: memset(count,-1, sizeof(count));
21: continue;
22: }
23: count[index] = i;
24: }
25: if(maxV < (s.size() -start))
26: {
27: maxV = s.size()-start;
28: }
29: return maxV;
30: }
Version 2, refactor the code at 3/4/2013
1: int lengthOfLongestSubstring(string s) {
2: int count[26];
3: memset(count,-1
, 26*sizeof(int));
4: int len=0, maxL = 0;
5: for(int i =0; i< s.size(); i++,len++)
6: {
7: if(count[s[i]-'a']>=0)
8: {
9: maxL = max(len, maxL);
10: len =0;
11: i = count[s[i]-'a']+1;
12: memset(count,-1
, 26*sizeof(int));
13: }
14: count[s[i]-'a']=i;
15: }
16: returnmax(len, maxL)
;
17: }
Note:
1. Line 3, since we store the index in array, so initializing the array as 0 will mistake the logic.
2. Line 16, catch the last string. for example, “abcd”, if no Line 16, it will just return 0 since the Line 9 won’t be triggered.
0 0
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
- LeetCode OJ 之 Longest Substring Without Repeating Characters 解题报告
- Leetcode (3) Longest Substring Without Repeating Characters 解题报告
- [leetcode] 3. Longest Substring Without Repeating Characters 解题报告
- [LeetCode] Longest Substring Without Repeating Characters 解题报告
- Leetcode 3. Longest Substring Without Repeating Characters解题报告
- [LeetCode] Longest Substring Without Repeating Characters 解题报告
- [Leetcode] 3. Longest Substring without Repeating Characters 解题报告
- leetcode解题报告3. Longest Substring Without Repeating Characters
- leetcode:Longest Substring Without Repeating Characters解题报告
- [LeetCode 解题报告]003.Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters 解题报告
- 【LeetCode】3.Longest Substring Without Repeating Characters解题报告
- LeetCode(3) Longest Substring Without Repeating Characters解题报告
- Leetcode Longest Substring Without Repeating Characters解题报告
- Leetcode #3 Longest Substring Without Repeating Characters 解题小结
- 【Leetcode】3.Longest Substring Without Repeating Characters 解题
- [Leetcode]解题文档-Longest Substring Without Repeating Characters
- [Leetcode] Length of Last Word 解题报告
- [LeetCode] Letter Combinations of a Phone Number 解题报告
- [LeetCode] Longest Common Prefix 解题报告
- [LeetCode] Longest Palindromic Substring 解题报告
- Android网络缓存的实现思路
- [LeetCode] Longest Substring Without Repeating Characters 解题报告
- [LeetCode] Maximum Depth of Binary Tree
- [LeetCode] Maximum Subarray 解题报告
- Windows Server 2008 R2 With SP1简体中文版 + 破解补丁
- [LeetCode] Median of Two Sorted Arrays 解题报告
- [LeetCode] Merge k Sorted Lists 解题报告
- [LeetCode] Merge Sorted Array 解题思路
- 调用活动,通过intent显式调用
- du&df