Last active
December 28, 2015 09:28
-
-
Save safeng/7478656 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int lengthOfLongestSubstring(string s) { | |
// IMPORTANT: Please reset any member data you declared, as | |
// the same Solution instance will be reused for each test case. | |
int bitmap[256]; | |
std::memset(bitmap,0,sizeof(bitmap)); | |
int maxLen = 0; | |
int curLen = 0; | |
for(int i = 0; i<(int)s.length();) | |
{ | |
char c = s[i]; | |
if(bitmap[c]) | |
{ | |
i = bitmap[c]; // start from the next | |
std::memset(bitmap,0,sizeof(bitmap)); | |
if(curLen>maxLen) | |
maxLen = curLen; | |
curLen = 0; | |
}else | |
{ | |
bitmap[c] = i+1; | |
++curLen; | |
++i; | |
} | |
} | |
if(curLen>maxLen) | |
maxLen = curLen; | |
return maxLen; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxArea(vector<int> &height) { | |
// IMPORTANT: Please reset any member data you declared, as | |
// the same Solution instance will be reused for each test case. | |
size_t i = 0, j = height.size()-1; | |
size_t maxSize = 0; | |
while(i<j) | |
{ | |
size_t curSize = 0; | |
if(height[i]<height[j]) | |
{ | |
curSize = height[i]*(j-i); | |
if(curSize > maxSize) | |
maxSize = curSize; | |
++i; | |
}else | |
{ | |
curSize = height[j]*(j-i); | |
if(curSize > maxSize) | |
maxSize = curSize; | |
j--; | |
} | |
} | |
return (int)maxSize; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment