LeetCode:Longest Substring Without Repeating Characters(update)

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.

运行时间64ms map初始化 待优化

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         
 5         unordered_map<char,int> last;
 6         int start=0;
 7         int max_len;
 8         
 9         for(int i=0;i<s.size();i++)
10         {
11             last[s[i]]=-1;
12         }
13         
14         for(int i=0;i<s.size();i++)
15         {
16             if(last[s[i]]>=start)
17             {
18                 max_len=max(i-start,max_len);
19                 start=last[s[i]]+1;
20             }
21             last[s[i]]=i;
22         }
23        
24        return max((int)s.size()-start,max_len); 
25         
26     }
27 };

 

posted @ 2015-08-19 21:42  尾巴草  阅读(137)  评论(0编辑  收藏  举报