码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Substring Without Repeating Characters

时间:2015-09-07 22:50:00      阅读:213      评论: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.

 

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4        int maxlen = 0, left = 0;  
 5        int sz = s.length();  
 6        int prev[256];  
 7        memset(prev, -1, 256*sizeof(int));  
 8   
 9        for (int i = 0; i < sz; i++) {  
10            if (prev[s[i]] >= left) {  
11                left = prev[s[i]] + 1;  
12            }  
13            prev[s[i]] = i;  
14            maxlen = max(maxlen, i - left + 1);  
15        }  
16        return maxlen;  
17     }
18 };

 

Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/hexhxy/p/4790115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!