星期一, 五月 20, 2013

Longest Substring Without Repeating Characters

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        // Start typing your Java solution below
        // DO NOT write main() function
        HashSet set = new HashSet();
        String ret = new String();
        int i=0,j=0;
        for(i=0;i            if (ret.length() > s.length() - i) return ret.length();
           
            set.add(s.charAt(i));
            for(j=i+1;j                if (set.contains(s.charAt(j))) {
                    set.clear();
                    break;
                }
                else{
                    set.add(s.charAt(j));
                }
            }
           
            if (j - i + 1 > ret.length()) ret = s.substring(i,j);
        }
       
        return ret.length();
    }
}

没有评论:

发表评论