3.Longest Substring Without Repeating Characters

来源:互联网 发布:allrightsreserved软件 编辑:程序博客网 时间:2024/06/08 18:33

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.

题意:求一个字符串的最大连续不包含相同字符的串的长度。

原创代码:

class Solution {public:    int lengthOfLongestSubstring(string s) {vector<char> storage;bool flag = false;        int max_length = 0;int length = s.length();for(int i = 0;i < length;i++){if(i == 0){storage.push_back(s[i]);max_length = 1;}else{for(vector<char>::iterator iter = storage.begin();iter != storage.end();iter++){if(s[i] == *iter){flag = true;break;}}if(!flag){storage.push_back(s[i]);if(storage.size() > max_length) max_length = storage.size();}else{//if(storage.size() > max_length) max_length = storage.size();int temp = 1;while(storage.back() != s[i]){storage.pop_back();temp++;}i -= temp;storage.clear();flag = false;}}}return max_length;    }};



原创粉丝点击