leetcode: Longest Substring Without Repeating Characters
来源:互联网 发布:java静态变量初始化 编辑:程序博客网 时间:2024/05/21 06:16
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)
class Solution {public: int lengthOfLongestSubstring(string s) { int used[256]; memset( used, 0, sizeof(used)); int tmp = 0, maximum = 0; for( int i = 0; i < s.size(); ++i){ if( used[s[i]] > 0){ i = used[s[i]] - 1; memset( used, 0, sizeof( used));//这里注意不要大意 maximum = max( maximum, tmp); tmp = 0; } else{ used[s[i]] = i + 1; ++tmp; } } return max( maximum, tmp);//别忘了最后一次 }};
0 0
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: 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 Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] 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
- PHP获取post中的json数据
- C/C++内存管理和指针的基础知识
- Closure Library 1 -- Dependence Management
- Java与字符编码问题详谈
- QT编程器
- leetcode: Longest Substring Without Repeating Characters
- java.lang.UnsatisfiedLinkError: Couldn't load locSDK3
- printf "%.*s"
- Win8.1无法安装.net framework3.5的解决方法
- 移动互联网缩写
- 经纬财富:铜川现货交易的地位和作用
- C/C++内存管理和指针基础知识2
- WM exiting with result code 0, clean up skipped
- Bandwidth +uva +回溯