Longest Substrings Without Repeating Characters
2015-09-03 01:34
435 查看
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.
Analyse: set two pointers, index1 and index2. When index2 is less than the length of the string, check interval s[index1...index2] and find whether there is duplicate number with s[index2]. If yes, index1++, else index2++. Remember to update the result.
Runtime: 20ms.
Analyse: set two pointers, index1 and index2. When index2 is less than the length of the string, check interval s[index1...index2] and find whether there is duplicate number with s[index2]. If yes, index1++, else index2++. Remember to update the result.
Runtime: 20ms.
class Solution { public: int lengthOfLongestSubstring(string s) { if(s.length() <= 1) return s.length(); int index1 = 0, index2 = 1; int result = INT_MIN; for(; index2 < s.length(); index2++){ for(int i = index1; i < index2; i++){ if(s[i] == s[index2]){ index1 = i + 1; break; } } result = max(result, index2 - index1 + 1); } return result; } };
相关文章推荐
- [LeetCode#43]Multiply Strings
- CSS3中的字体rem
- HDU1031 - Design T-Shirt (水题 模拟)
- Python中的元类(metaclass)
- 08-27作业
- Java项目图标前面有红色叹号
- Fibonacci(斐波那契)序列学习总结
- 修正Lilypond生成简谱工具
- 前端工程师技能之photoshop巧用(从效果图到雪碧图)2——测量篇
- 导入SDK中自带的Demo
- LeetCode Insertion Sort List
- LeetCode Insertion Sort List
- iOS 8 Swift Programming Cookbook
- samba服务
- iscsi
- [2015/09/03] 快速幂算法 - 大数取余
- poj 3667(区间合并)
- RAID-10 阵列的创建(软)
- DedeCMS模板文件不存在,无法解析文档! 问题定位方法
- 如何在Windows7操作系统下安装用于C++开发的Eclipse