class Solution {
public:
/**
* @param s: a string
* @return: an integer
*/
int lengthOfLongestSubstring(string &s) {
// write your code here
if(s.empty()) return 0;
if(s.size()==1) return 1;
string temp="";
int maxSize=0;
for(int i=0; i<s.size(); i++){
if(temp.find(s[i])>temp.size()){
temp.push_back(s[i]);
}else{
if(maxSize<temp.size()) maxSize=temp.size();
int len = temp.size();
temp.clear();
i = i-len+1;
temp.push_back(s[i]);
}
}
if(maxSize<temp.size()) maxSize = temp.size();
return maxSize;
}
};
Lintcode384. 最长无重复字符的子串 C++
最新推荐文章于 2020-05-15 20:50:55 发布