zoukankan      html  css  js  c++  java
  • LeetCode 1286. Iterator for Combination

    原题链接在这里:https://leetcode.com/problems/iterator-for-combination/

    题目:

    Design the CombinationIterator class:

    • CombinationIterator(string characters, int combinationLength) Initializes the object with a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
    • next() Returns the next combination of length combinationLength in lexicographical order.
    • hasNext() Returns true if and only if there exists a next combination.

    Example 1:

    Input
    ["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
    [["abc", 2], [], [], [], [], [], []]
    Output
    [null, "ab", true, "ac", true, "bc", false]
    
    Explanation
    CombinationIterator itr = new CombinationIterator("abc", 2);
    itr.next();    // return "ab"
    itr.hasNext(); // return True
    itr.next();    // return "ac"
    itr.hasNext(); // return True
    itr.next();    // return "bc"
    itr.hasNext(); // return False

    Constraints:

    • 1 <= combinationLength <= characters.length <= 15
    • All the characters of characters are unique.
    • At most 104 calls will be made to next and hasNext.
    • It's guaranteed that all calls of the function next are valid.

    题解:

    It comes to how to find the next combination. Say characters = "abcd", current combination = "acd".

    1. Remove the common suffix with characters, which is "cd".
    2. Now current String = "a". Remove the right most char, which is 'a'. current String = "". Find the index of a in characters, which is 0.
    3. Increment that index by 1, 0++. It points to 'b'. Append this char and the following until current combination length = combinationLength.

    Could use StringBuilder to maintain the current combination.

    Time Complexity: CombinationIterator, O(n). next, O(n). hasNext, O(1). n = characters.length().

    Space: O(n).

    AC Java:

     1 class CombinationIterator {
     2     StringBuilder sb;
     3     String s;
     4     int len;
     5     HashMap<Character, Integer> hm;
     6 
     7     public CombinationIterator(String characters, int combinationLength) {
     8         this.sb = new StringBuilder();
     9         this.s = characters;
    10         this.len = combinationLength;
    11         this.hm = new HashMap<>();
    12         
    13         for(int i = 0; i<s.length(); i++){
    14             char c = s.charAt(i);
    15             if(sb.length() < len){
    16                 sb.append(c);
    17             }
    18             
    19             hm.put(c, i);
    20         }
    21     }
    22     
    23     public String next() {
    24         String res = sb.toString();
    25         int ind = s.length() - 1;
    26         while(ind >= 0 && sb.length() != 0 && sb.charAt(sb.length() -1) == s.charAt(ind)){
    27             sb.deleteCharAt(sb.length() - 1);
    28             ind--;
    29         }
    30         
    31         if(sb.length() == 0){
    32             return res;
    33         }
    34         
    35         char c = sb.charAt(sb.length() - 1);
    36         ind = hm.get(c) + 1;
    37         sb.deleteCharAt(sb.length() - 1);
    38         
    39         while(sb.length() < len && ind < s.length()){
    40             sb.append(s.charAt(ind));
    41             ind++;
    42         }
    43         
    44         return res;
    45     }
    46     
    47     public boolean hasNext() {
    48         return sb.length() != 0;
    49     }
    50 }
    51 
    52 /**
    53  * Your CombinationIterator object will be instantiated and called as such:
    54  * CombinationIterator obj = new CombinationIterator(characters, combinationLength);
    55  * String param_1 = obj.next();
    56  * boolean param_2 = obj.hasNext();
    57  */

    类似Next Permutation.

  • 相关阅读:
    例行更新,防止被踢
    C语言 遍历磁盘目录
    析构函数的调用
    数组学习笔记
    函数学习笔记
    c++语言 纯虚函数的使用
    c++语言 内联方法
    复制构造函数
    c++语言 覆盖成员函数
    面向对象程序设计
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/14494353.html
Copyright © 2011-2022 走看看