• [LeetCode#159] Missing Ranges Strobogrammatic Number


    Problem:

    Given a string, find the length of the longest substring T that contains at most 2 distinct characters.

    For example, Given s = “eceba”,

    T is "ece" which its length is 3.

    Analysis:

    This is a very very typical question in using slide window. Why?
    The problem asks the substring to meet certain characteristic. The substring means the target must be a series of succsive characters. 
    
    Basic idea:
    The problem restricts the target must contain no more than 2 distinct characters. 
    Apparently we should use a HashMap to record such information. (when there is a quantitive restriction, you should firstly think about using HashMap rather than HashSet)
    
    Weaving Picature:
    Maintain two boundary for a window : "front" and "end", recording the relevent information(according problems' requirement and restriction) through a HashMap. 
    
    For this problem, we maintain "HashMap<Character, Integer>" to record Character's count in the window. It means we at most allow two distinct Characters in the HashMap. When the end pointer was moved onto a new character.
    case1. iff the charcter existed in the HashMap, we can directly include it into our current window.
    case2. iff the charcter is not existed in the HashMap, we may have to adjust the start pointer of slide window to make it is valid to add the new charcter into the window.
    Note: you should not hesitate over wheather to use hashmap's size() or "hashmap.contains(c)" as the first level "if-else" checking. Since we may not need to care the hashmap's size when case1.
    ---------------------------------------------------------------------
    if (!map.containsKey(c)) {
        ...
    }else {
        ...
    }
    
    For case 2.
    a. Iff the map's size is less than 2, we can directly include it into window.
    if (map.size() < 2) {
        map.put(c, 1);
        end = i;
    }
    
    b. Iff the map's size is equal to 2, we need to adjust the window's start boundary.
    Skill: keep on moving start boundary, until one character was totally wiped out from the window. 
    while (map.size() == 2) {
        char discard = s.charAt(start);
        start++;
        map.put(discard, map.get(discard) - 1);
        if (map.get(discard) == 0)
            map.remove(discard);
    }
    map.put(c, 1);
    end = i;
    Note: we don't need to care wheather "start" would exceed the s.length, since when there is only one character in the window, it would exit the while loop. This is beautiful part in using while-loop in slide window.

    Solution 1:

    public class Solution {
        public int lengthOfLongestSubstringTwoDistinct(String s) {
            if (s == null)
                throw new IllegalArgumentException("s is null");
            int len = s.length();
            if (len <= 2)
                return len;
            int start = 0, end = 0, max = 0;
            HashMap<Character, Integer> map = new HashMap<Character, Integer> ();
            for (int i = 0; i < len; i++) {
                char c = s.charAt(i);
                if (!map.containsKey(c)) {
                    if (map.size() < 2) {
                        map.put(c, 1);
                        end = i;
                    } else{
                        while (map.size() == 2) {
                            char discard = s.charAt(start);
                            start++;
                            map.put(discard, map.get(discard) - 1);
                            if (map.get(discard) == 0)
                                map.remove(discard);
                        }
                        map.put(c, 1);
                        end = i;
                    }
                } else{
                    map.put(c, map.get(c) + 1);
                    end = i;
                }
                max = Math.max(max, end - start + 1);
            }
            return max;
        }
    }

    Improvement Analysis:

    Even my first solution is right, there are many room for improving it regarding the elegance of code.
    1. When we use slide window, "i" is actually the end boundary of slide window. We don't need to maintain a end variable. And we always measure the window, after it was adjusted into valid. (i is not change!)
    max = Math.max(max, i - start + 1);
    
    2. The purpose of adjusting window is to make the current end boundary valid. And after the adjust (or no need for the adjust), we finally need to put the character at "end" into the map. 
    while (map.size() == 2) {
        char discard = s.charAt(start);
        start++;
        map.put(discard, map.get(discard) - 1);
        if (map.get(discard) == 0)
            map.remove(discard);
    }
    map.put(c, 1);

    Solution 2:

    public class Solution {
        public int lengthOfLongestSubstringTwoDistinct(String s) {
            if (s == null)
                throw new IllegalArgumentException("s is null");
            int len = s.length();
            if (len <= 2)
                return len;
            int start = 0, end = 0, max = 0;
            HashMap<Character, Integer> map = new HashMap<Character, Integer> ();
            for (int i = 0; i < len; i++) {
                char c = s.charAt(i);
                if (!map.containsKey(c)) {
                    while (map.size() == 2) {
                        char discard = s.charAt(start);
                        start++;
                        map.put(discard, map.get(discard) - 1);
                        if (map.get(discard) == 0)
                            map.remove(discard);
                    }
                    map.put(c, 1);
                } else{
                    map.put(c, map.get(c) + 1);
                }
                max = Math.max(max, i - start + 1);
            }
            return max;
        }
    }
  • 相关阅读:
    Django rest_framework实现增删改查接口
    文件的三种打开方式知识点回顾
    Django中基表的创建、外键字段属性简介、脏数据概念、子序列化
    drf序列化与反序列化作业1
    rest_framework序列化与反序列化1
    APIview的请求生命周期源码分析
    sql
    正则上面的一个坑
    多线程与多进程---方法对比与使用
    网络编程----踩坑篇
  • 原文地址:https://www.cnblogs.com/airwindow/p/4797278.html
Copyright © 2020-2023  润新知