LeetCode------Longest Substring Without Repeating Characters

原创 2015年07月10日 11:35:42

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

分析:题目的意思很简单,找出字符串中连续不重复字母的最大个数。 该题目最适使用贪心算法,即找出每一个以某个位置开始的最大连续字母个数,然后找出这些最大个数中的最大值即我们要返回的最长连续不重复字母的长度。

此处提供三个版本,这个问题最容易犯的错误就是找一次之后,第二次以后的开始位置,这里应该设置一个标志,标示从哪里开始计数查找的,下一次的时候这个标志前移一位即可。

最优版本:

public static int lengthOfLongestSubstring(String s) {
             int maxLen = 0;
             int currentLen = 0;
             int start = 0;
             int hash[] = new int[256];
             for(int i=0; i<s.length(); i++){
                 int lastPost = hash[ s.charAt(i) ];
                 if(lastPost == 0){
                     currentLen++;
                 }else{
                     for(int j=start; j< lastPost; j++)
                        hash[ s.charAt(j) ] = 0;
                     start = lastPost ;
                     currentLen = i - start + 1;
                 }
                 hash[s.charAt(i)] = i+1;
                 if(maxLen < currentLen) maxLen = currentLen;
             }
             return maxLen;
        }

第二种版本:

    public static int lengthOfLongestSubstring(String s) {
        Set<String> set=new HashSet<String>();
        char[] strChar=s.toCharArray();
        int maxlength=0;
        int indexFlag=0;
        for(int i=0;i<strChar.length;i++){
            if(set.size()==0){
                indexFlag=i;
            }
            if(set.contains(String.valueOf(strChar[i]))){
                set.clear();
                set.add(String.valueOf(strChar[i]));
                i=indexFlag+1;
                indexFlag=i;
            }else{
                set.add(String.valueOf(strChar[i]));
            }
            int tempSize=set.size();
            if(tempSize>maxlength){
                maxlength=tempSize;
            }
        }
        return maxlength;
    }

第三个版本:

    public static int lengthOfLongestSubstring(String s) {
        if(""==s||null==s){
            return 0;
        }
        char[] strArr=s.toCharArray();
        Hashtable<String, Integer> table=new Hashtable<String, Integer>();
        int maxSize=0;
        int tempintSize=0;
        int tempindex=0;
        for(int i=0;i<strArr.length;i++){
            Integer value= table.get(String.valueOf(strArr[i]));
            if(value==null&&table.size()==0){
                table.put(String.valueOf(strArr[i]),i);
                tempindex=i;
            }else if(value==null&&table.size()!=0){
                table.put(String.valueOf(strArr[i]),i);
            }
            else{
                table.clear();
                i=tempindex+1;
//              System.out.println(i);
                table.put(String.valueOf(strArr[i]),i);
                tempindex=i;
            }
            tempintSize=table.size();
            if(tempintSize>maxSize){
                maxSize=tempintSize;
            }
        }
        return maxSize;
    }

这个题目另一种优化思路是:假设某个位置出现了重复的字母,那么包含这个位置的所有组合就不用再判断了,即子问题决定父问题。

版权声明:本文为博主原创文章,未经博主允许不得转载。

【LeetCode-面试算法经典-Java实现】【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】

【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】给定一个字符串,找字符中的最大非重复子串用start记录当前处理的开始位置...

【LeetCode】Longest Substring Without Repeating Characters

问题描述Given a string, find the length of the longest substring without repeating characters.Examples:G...

[LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters

1 问题 Given a string, find the length of the longest substring without repeating characters. Exa...

Longest Substring Without Repeating Characters-----LeetCode

题目Given a string, find the length of the longest substring without repeating characters.Examples:Giv...

leetcode003 Longest Substring Without Repeating Characters

题目3. Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

leetcode: Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

(leetcode) Longest Substring Without Repeating Characters

Question : Given a string, find the length of the longest substring without repeating characters. ...

LeetCode- 3. Longest Substring Without Repeating Characters

Description Given a string, find the length of the longest substring without repeating characters.E...

leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters标签(空格分隔): leetcode string twoPoints setGi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode------Longest Substring Without Repeating Characters
举报原因:
原因补充:

(最多只允许输入30个字)