Last active
December 14, 2015 13:38
-
-
Save guolinaileen/5094585 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
int length=s.length(); | |
bool exist[256]={false}; | |
int start=0; | |
int maxL=0; | |
for(int i=0; i<length; i++) | |
{ | |
if(exist[s[i]]) | |
{ | |
maxL=max(maxL, i-start); | |
while(s[start]!=s[i]) | |
{ | |
exist[s[start]]=false; | |
start++; | |
} | |
start++; | |
}else | |
{ | |
exist[s[i]]=true; | |
} | |
} | |
return max(maxL, length-start); | |
} | |
}; |
This file contains hidden or 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
public class Solution { | |
public int lengthOfLongestSubstring(String s) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int length=s.length(); | |
boolean [] exists=new boolean [256]; | |
int start=0, max=0; | |
for(int i=0; i<length; i++) | |
{ | |
if(!exists[s.charAt(i)]) | |
{ | |
exists[s.charAt(i)]=true; | |
}else | |
{ | |
max=Math.max(max, i-start); | |
while(s.charAt(start)!=s.charAt(i)) | |
{ | |
exists[s.charAt(start)]=false; start++; | |
} | |
start++; | |
} | |
} | |
max=Math.max(max, length-start); | |
return max; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment