[LeetCode] 3. Longest Substring Without Repeating Characters

首先看一下只需要return 长度的解法:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        /***
         * Basic idea: N(o^2): for each character, continue looping and store it in a set util it meets the same character, 
         * set the break then and calculate the length.
         ***/
        
        int longest = 0;
        for(int i = 0; i < s.length(); i++){
            Set setChars = new HashSet();
            for (int j = i; j < s.length(); j++){
                if(setChars.contains(s.charAt(j))){
                    break;
                }else {
                    setChars.add(s.charAt(j));
                }
            }
            longest = longest > setChars.size()? longest: setChars.size();
        }
        return longest;
    }
}

继续学习一下这个网页中的四种解法:

http://www.cnblogs.com/TenosDoIt/p/3675788.html 

posted on 2015-12-10 13:06  codingEskimo  阅读(123)  评论(0编辑  收藏  举报

导航