03_Longest Substring Without Repeating Characters

1.Question

2.Solution

public static int lengthOfLongestSubstring(String s) {
        int max = 0;
        int len = s.length();
        int i = 0 , j = 1;
        while(i < len && j <= len){
            Set item = new HashSet<Character>();
            String temp = s.substring(i,j++);
            for(int k = 0; k < temp.length(); k++){
                if(!item.contains(temp.charAt(k))){
                    item.add(temp.charAt(k));
                    max = max > item.size()? max:item.size();        
                }else{
                    break;
                }        
            }
            if(j > len){
                i ++;
                j = i;
            }    
        }
        return max;
    }

 

3.Test

posted @ 2016-06-19 14:47  桃源仙居  阅读(139)  评论(0)    收藏  举报