首页 > 其他 > 详细

Longest Substrings Without Repeating Characters

时间:2015-09-03 01:50:59      阅读:242      评论:0      收藏:0      [点我收藏+]

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.

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         if(s.length() <= 1) return s.length();
 5         
 6         int index1 = 0, index2 = 1;
 7         int result = INT_MIN;
 8         for(; index2 < s.length(); index2++){
 9             for(int i = index1; i < index2; i++){
10                 if(s[i] == s[index2]){
11                     index1 = i + 1;
12                     break;
13                 }
14             }
15             result = max(result, index2 - index1 + 1);
16         }
17         return result;
18     }
19 };

 

Longest Substrings Without Repeating Characters

原文:http://www.cnblogs.com/amazingzoe/p/4779823.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!