首页 > 其他 > 详细

LeetCode 03: Longest Substring Without Repeating Characters

时间:2015-06-02 09:24:26      阅读:163      评论: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.


代码如下:

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

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

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