public int lengthOfLongestSubstring(String s) {
if(s.length() == 0) {
return 0;
}
int[] flag = new int[128];
int len = s.length();
int i = 0, j = 0;
int max = 1, count = 0;
for(i = 0; i < 128; i++) {
flag[i] = -1;
}
for(i = 0; i < len; i++) {
count = 1;
int c = (int)s.charAt(i);
flag[c] = i;
for(j = i+1; j < len; j++) {
int c2 = (int)s.charAt(j);
if(flag[c2] == i) {
count = j - i;
break;
}else {
flag[c2] = i;
}
}
if( count > max) {
max =count;
}else if(j >=len && (len -i) > max) {
max = len -i;
}
}
return max;
}
3. Longest Substring Without Repeating Characters
最新推荐文章于 2024-08-06 17:50:14 发布