Skip to content

Instantly share code, notes, and snippets.

@safeng
Last active December 28, 2015 09:28
Show Gist options
  • Save safeng/7478656 to your computer and use it in GitHub Desktop.
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.
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;
}
};
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