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

LeetCode 03: Longest Substring Without Repeating Characters

时间:2015-06-02 09:24:48      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:leetcode   longest substring   

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.


代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        bool d[128] = { false };  
		int max_len = 0;  
		int start = 0;
		int size = s.size();
		char c;
		for(int i=0; i<size; i++) {  
			c = s[i];  
			if(!d[c]) {  
				d[c] = true;  
				max_len = (max_len>i-start+1) ? max_len:i-start+1;   
			} else {  
				while(s[start] != c) {  
					d[s[start]] = false;  
					++start;  
				}  
				++start;  
			}  
		}  
		return max_len;  
    }
};


LeetCode 03: Longest Substring Without Repeating Characters

标签:leetcode   longest substring   

原文地址:http://blog.csdn.net/sunao2002002/article/details/46318301

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