public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s==null||s.length()==0){
return 0;
}
final int LEN=256;
int[] indexOfChar=new int[LEN];
for(int i=0;i<LEN;i++){
indexOfChar[i]=-1;
}
char[] charStr = s.toCharArray();
int max=0,begin=0,tmp=0;
for(int i=0;i<s.length();i++){
if(indexOfChar[charStr[i]]<begin){
tmp++;
}else{
max=max>tmp?max:tmp;
tmp=i-indexOfChar[charStr[i]];
begin=indexOfChar[charStr[i]]+1;
}
indexOfChar[charStr[i]] = i;
}
max=max>tmp?max:tmp;
return max;
}
}
Longest Substring Without Repeating Characters
最新推荐文章于 2023-10-08 01:10:54 发布