欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

LeetCode Longest Substring Without Repeating Characters,leetcoderepeating

来源: javaer 分享于  点击 47377 次 点评:256

LeetCode Longest Substring Without Repeating Characters,leetcoderepeating


Longest Substring Without Repeating Characters

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) {
    	int len = s.size();
		if(len <= 1)	return len;
		vector<bool> chars(256, false);
		vector<int> position(256, 0);
		int left = 0;
		int cur = 0;
		int ret = 0;
		while(cur < len)
		{
			char ch = s.at(cur);
			if(!chars[ch])
			{
				chars[ch] = true;
				position[ch] = cur;
			}
			else
			{
				ret = max(ret, cur - left);
				for(int i = left; i < position[ch]; ++i)
					chars[s.at(i)] = false;
				left = position[ch] + 1;
				position[ch] = cur;
			}
			++cur;
		}
		return max(ret, cur - left);
    }
};

相关文章

    暂无相关文章

用户点评