leetcode之Longest Substring Without Repeating Characters
2013-09-09 16:06
399 查看
class Solution {
private:
bool canUse[256];
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s.size() <2) {
return s.size();
}
int max = INT_MIN;
map<int, int> mymap;
mymap[s[0]] = 1;
int temp = 1;
for (int i = 1; i<s.size(); i++) {
if (mymap[s[i]] == 0) {
mymap[s[i]] = i+1;
temp++;
}
else {
if (temp >= max) {
max=temp;
}
temp = 1;
i = mymap[s[i]];
mymap.clear();
mymap[s[i]]=i+1;
}
if (temp >= max) {
max=temp;
}
}
return max;
}
};
private:
bool canUse[256];
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s.size() <2) {
return s.size();
}
int max = INT_MIN;
map<int, int> mymap;
mymap[s[0]] = 1;
int temp = 1;
for (int i = 1; i<s.size(); i++) {
if (mymap[s[i]] == 0) {
mymap[s[i]] = i+1;
temp++;
}
else {
if (temp >= max) {
max=temp;
}
temp = 1;
i = mymap[s[i]];
mymap.clear();
mymap[s[i]]=i+1;
}
if (temp >= max) {
max=temp;
}
}
return max;
}
};
相关文章推荐
- LeetCode 03: Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode题解——Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters -leetcode
- Leetcode [Longest Substring Without Repeating Characters]
- leetcode---longest-substring-without-repeating-characters---字符串
- LeetCode Longest Substring Without Repeating Characters
- leetcode 3:Longest Substring Without Repeating Characters
- 【编程】leetcode_longest substring without repeating characters
- [LeetCode]3 Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters 解题报告
- [Leetcode]_3 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- LeetCode_Longest Substring Without Repeating Characters
- leetcode-Longest Substring Without Repeating Characters
- 【leetcode】Longest Substring Without Repeating Characters
- [Leetcode]Longest Substring Without Repeating Characters
- leetcode__Longest Substring Without Repeating Characters
- leetcode -- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters (leetcode3)