zoukankan      html  css  js  c++  java
  • Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters

    问题:

    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.

    思路:

      two point + hashtable

    我的代码:(未通过,超时,(⊙﹏⊙)b)

    import java.util.Hashtable;
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            if(s == null || s.length() == 0)    return 0;
            Hashtable<Character,Integer> ht = new Hashtable<Character,Integer>();
            int rst = 0;
            int max = 0;
            int i = 0;
            while(i < s.length())
            {
                char c = s.charAt(i);
                if(!ht.containsKey(c))
                {
                    ht.put(c, i);
                    max++;
                    rst = Math.max(max,rst);
                    i++;
                }
                else
                {
                    int pastKey = ht.get(c);
                    ht.clear();
                    max = 0;
                    i = pastKey + 1;
                }
            }
            return rst;
        }
    }
    View Code

     他人代码1:

    import java.util.Hashtable;
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            if(s == null || s.length() == 0)    return 0;
            Hashtable<Character,Integer> ht = new Hashtable<Character,Integer>();
            int max = 0;
            int left = 0;
            for(int right = 0; right < s.length(); right++)
            {
                char c = s.charAt(right);
                if(ht.containsKey(c) && ht.get(c) >= left)
                {
                    left = ht.get(c) + 1;
                }
                ht.put(c, right);
                max = Math.max(max, right - left + 1);
            }
            return max;
        }
    }
    View Code

    他人代码2:(用数组代替HashMap)

    public int lengthOfLongestSubstring(String s) {
            if (s == null) {
                return 0;
            }
            
            int max = 0;
            
            // suppose there are only ASCII code.
            int[] lastIndex = new int[128];
            for (int i = 0; i < 128; i++) {
                lastIndex[i] = -1;
            }
            
            int len = s.length();
            int l = 0;
            for (int r = 0; r < len; r++) {
                char c = s.charAt(r);
                
                if (lastIndex[c] >= l) {
                    l = lastIndex[c] + 1;
                }
                
                // replace the last index of the character c.
                lastIndex[c] = r;
                
                // replace the max value.
                max = Math.max(max, r - l + 1);
            }
            
            return max;
        }
    View Code

    学习之处:

    • 想的太复杂,没有抓住问题的实质,平常写代码也是,先考虑!ht.containsKey() 是不是也可以先考虑 ht.containsKey的情况呢。
    • ASCLL是一个字节,是8位,而最高位是用来进行奇偶校验的,所以可以表示的数字有27=128 故可以使用128的数组进行存储,代替HashMap。
    • 双指针体现在,一个在前走着,确定上界(上限),后面的那个用来确定下界(下限) 此为上下界中的双指针问题。
  • 相关阅读:
    0/1背包问题
    假如爱有天意(中文版)
    tomcat集群
    分布式锁
    centos7安装Harbor(转载)
    isEmpty和isBlank的区别
    单体应用架构和分布式架构的比较
    微服务鉴权
    mysql的时区设置
    RSA非对称加密算法
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4340982.html
Copyright © 2011-2022 走看看