1. 程式人生 > >leetcode3-Longest Substring Without Repeating Characters

leetcode3-Longest Substring Without Repeating Characters

class Solution {
     public int lengthOfLongestSubstring(String s) {
        if (s== null || s.length() == 0){
            return 0;
        }
        int max = 0;
        int temp = 0;
        char c[] = s.toCharArray();
        for(int i = 0;i<c.length;i++){
            Set<Character> set = new HashSet<Character>();
            temp = 0;
            for(int j = i;j<c.length;j++){
                if(set.contains(c[j])){
                    break;
                }else{
                    set.add(c[j]);
                    temp++;
                }
            }
            if (max<temp){
                max = temp;
            }
        }
        return max;

    }
}