LeetCode lengthOfLongestSubstring

题目:

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

Examples:

  • Given “abcabcbb”, the answer is “abc”, which the length is 3.

  • Given “bbbbb”, the answer is “b”, with the length of 1.

  • Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

这道题我弄了三个小时。。。

一开始用了list,但写着写着发现顺序排列的下标不是我想要的,根本无法得到数组的索引,因此果断换HashMap,代码如下:

public int lengthOfLongestSubstring(String s) {
            int head=0,length=1,max=1;
            Map<Integer,Character> map=new HashMap<Integer,Character>();
            if(s.equals(""))
                return 0;
            char[] m=s.toCharArray();           
            while(head<m.length-1){
                map.put(head, m[head]);
                if(!map.containsValue(m[++head]))
                    {length++;        
                    max=Math.max(max, length);  
                    }
                else
                {
                    for(Map.Entry<Integer, Character> entry:map.entrySet()){
                        if(m[head]==entry.getValue())
                        {
                            head=entry.getKey()+1;
                            break;
                        }
                    }
                    map.clear();           
                    length=1;
                }
            }
            return max;

        }

}

一运行,超时了。看着人家给的数据我就跪了,一眼望不到底。。一般这种测试数据出现了,只能说明人家就是为了淘汰掉你的第一想法,一定存在技巧防止一个或多个循环消失,或者无需遍历某个数据结构。想了想,我发现可以不用把head移动到重复值的下一个,直接从map里删掉重复值下标之前的所有内容即可。改后如下:

public int lengthOfLongestSubstring(String s) {
            int head=0,length=1,max=1,index=0;
            boolean end=false;
            Map<Integer,Character> map=new HashMap<Integer,Character>();
            if(s.equals(""))
                return 0;
            char[] m=s.toCharArray();           
            while(head<m.length && !end){
                map.put(head, m[head]);
                head++;
                if(head==m.length)
                {   
                    head--;
                    end=true;
                }
                if(head!=m.length && !map.containsValue(m[head]))
                    {length++;        
                    max=Math.max(max, length);  
                    }
                else
                {
                    for(Map.Entry<Integer, Character> entry:map.entrySet()){
                        if(m[head]==entry.getValue())
                            {index=entry.getKey();
                            break;
                            }
                    }
                    Iterator it=map.keySet().iterator();
                    while(it.hasNext()){
                        int key=(int) it.next();
                        if(key<=index)
                            {it.remove();
                            length--;
                            }
                    }
                    length++;                   
                }
            }
            return max;

        }

感觉复杂度小了很多。然而!!

又超时了。。

此时已经过去了两个多小时。。好吧我要看答案了。。我真的尽力了。。

其实看完答案我觉得我的思想已经对了,但复杂就复杂在Iterator上了。答案给的是直接用下标相减,确实没想到。理解了答案后我又跑来改自己的代码,感觉改的过程中发现自己很难把问题考虑全面,index为什么用max我也想了半天。。以下为AC代码。

public static int lengthOfLongestSubstring(String s) {
            int head=0,length=1,max=1,index=0;
            boolean end=false;
            Map<Character,Integer> map=new HashMap<Character,Integer>();
            if(s.equals(""))
                return 0;
            char[] m=s.toCharArray();           
            while(head<m.length && !end){
                map.put(m[head],head);
                head++;
                if(head==m.length)
                {   
                    head--;
                    end=true;
                }
                if(!map.containsKey(m[head]))
                    length++;
                else
                {
                    index=Math.max(index,map.get(m[head]));
                    length=head-index;
                }
                max=Math.max(max, length);
            }

            return max;

        }

啊!祭奠我的三小时~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值