首页 > 其他 > 详细

LeetCode-无重复字符的最长子串

时间:2019-12-28 09:05:13      阅读:93      评论:0      收藏:0      [点我收藏+]

记 无重复字符的最长子串

写了你妈2小时写出来的垃圾算法

    
     public static int lengthOfLongestSubstring(String s) {      
        char t[] = s.toCharArray();
        int count = 0;
        int max =0;
        
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();  //核心
        for(int i =0;i<s.length();i++) {
            if(map.get(t[i]-'a')==null) {
                map.put(t[i]-'a',i);
                map2.put(i,t[i]-'a');
                count++;
                
                if(count>max) {
                    max = count;
                }

                
            }else {
                
                count = i-map.get(t[i]-'a');  //核心
                System.out.println(map.get(t[i]-'a'));
                int k = map.get(t[i]-'a');                                          
                int temp = -1;   //核心清理
                for(int j =0; j<=k ;j++) {
                    if(map2.get(j)!=null) {
                        temp = map2.get(j);
                        map.remove(temp);
                        map2.remove(j);
                        map.put(t[i]-'a',i);
                        map2.put(i,t[i]-'a');
                        
                    }
                    temp = -1;
                }
            }
            System.out.println(count+"---"+i+"--"+t[i]);
        }
        System.out.println("--------------");
        return max;
    }

LeetCode-无重复字符的最长子串

原文:https://www.cnblogs.com/fyyxt/p/12110228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!