3. Longest Substring Without Repeating Characters

来源:互联网 发布:彩虹岛禁用小草网络 编辑:程序博客网 时间:2024/04/29 12:37
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;    }
0 0
原创粉丝点击