Leetcode - Longest Substring Without Repeating Characters
来源:互联网 发布:jquery数组转json对象 编辑:程序博客网 时间:2024/06/10 12:48
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
int lengthOfLongestSubstring(string s) { int ans= 0, l = 0 ; int map[256] = {0}; for(int i = 0 ; i < s.size(); i ++) { if(map[s[i]] != 0 && map[s[i]] >= l) { ans= (i - l) > ans ? (i - l) : ans; l = map[s[i]] ; } if( i + 1 - l > ans) ans= i + 1 - l ; map[s[i]] = i + 1 ; } return anw; }
看起来更加牛B 的代码是这个:
int lengthOfLongestSubstring(string s) { int n = s.size(), ans = 0; int index [128] = {0}; // current index of character // try to extend the range [i, j] for (int j = 0, l = 0; j < n; j++) { l = std::max(index[s[j]], l); ans = std::max(ans, j - l + 1); index[s[j]] = j + 1; } return ans; }
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
- NavigationView下Menu点击切换Fragment的卡顿解决方法
- GlobalAlloc函数
- VirtualAlloc函数
- wxpython出错原因
- 自定义事件(2016.6.23)
- Leetcode - Longest Substring Without Repeating Characters
- iOS开发之理解iOS中的MVC设计模式
- c++学习感悟
- Android DownloadManager 的使用
- 广播注册的两种方式
- C++感悟!
- 九度OJ - 1028 - 继续畅通工程
- FusionCharts参数的详细说明——按图标类型
- Combination Sum III