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。
    • 双指针体现在,一个在前走着,确定上界(上限),后面的那个用来确定下界(下限) 此为上下界中的双指针问题。
  • 相关阅读:
    ACM2114_S[I](1^3+2^3+3^3)
    幻源境第二十八章
    Java中间件之RMI及实例介绍 · zijian's blog
    玩转iOS开发:iOS中的GCD开发(三)
    浅谈在ES5环境下实现const
    concurrent包分析之Executor框架
    Inheritance Learning Note
    开发岗笔试基础题总结
    论文笔记[Slalom: Fast, Verifiable and Private Execution of Neural Networks in Trusted Hardware]
    logstash收集nginx日志
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4340982.html
Copyright © 2011-2022 走看看