【leetcode】1286. Iterator for Combination
题目如下:
Design an Iterator class, which has:
- A constructor that takes a string
charactersof sorted distinct lowercase English letters and a numbercombinationLengthas arguments.- A function next() that returns the next combination of length
combinationLengthin lexicographical order.- A function hasNext() that returns
Trueif and only if there exists a next combination.Example:
CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator. iterator.next(); // returns "ab"
iterator.hasNext(); // returns true
iterator.next(); // returns "ac"
iterator.hasNext(); // returns true
iterator.next(); // returns "bc"
iterator.hasNext(); // returns falseConstraints:
1 <= combinationLength <= characters.length <= 15- There will be at most
10^4function calls per test.- It's guaranteed that all calls of the function
nextare valid.
解题思路:全排列的问题,把所有排列预先计算出来即可。
代码 如下:
class CombinationIterator(object):
def __init__(self, characters, combinationLength):
"""
:type characters: str
:type combinationLength: int
"""
self.val = []
from itertools import combinations
for i in combinations(characters, combinationLength):
self.val.append(''.join(i))
def next(self):
"""
:rtype: str
"""
return self.val.pop(0)
def hasNext(self):
"""
:rtype: bool
"""
return len(self.val) > 0
# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()
【leetcode】1286. Iterator for Combination的更多相关文章
- 【LeetCode】 数相加组合 Combination Sum
描述 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- 2019牛客暑期多校训练营(第二场) - B - Eddy Walker 2 - BM算法
参考于: https://www.luogu.org/problemnew/solution/P4723 shadowice1984 (太难) https://www.cnblogs.com/zhgy ...
- 牛客 158D a-贝利福斯数
将所有形如ax+1的数称为a-贝利福斯数,其中x是正整数.一个a-贝利福斯数是a-贝利福斯素数,当且仅当它不能被分解成两个a-贝利福斯数的积.现在给出a,n,问有多少个 ≤ n的a-贝利福斯数可以被分 ...
- JSON函数表2
[class] Json::Reader [public] [将字符串或者输入流转换为JSON的Value对象] bool parse( const std::string &document ...
- application.properties参数详解
# ----------------------------------------# CORE PROPERTIES# --------------------------------------- ...
- Java HashMap、HashTable与ConCurrentHashMap
一.Java中数据存储方式最底层的两种结构 1.数组:存储空间连续,寻址迅速,增删较慢.(代表:ArrayList) 2.链表:存储空间不连续,寻址慢,增删较快.(代表:LinkedList) 二.哈 ...
- Js 更换html同一父元素下子元素的位置
//更换两个元素的位置 var exchange=function (el1, el2) { var ep1 = el1[0].parentNode, ep2 = el2[0].parentNode, ...
- C#异步编程学习笔记之-async和await
一.异步方法介绍(async和await):如果使用async修饰符将某种方法指定为异步方法,即启用以下两种功能.1.标记的异步方法可以使用await来指定暂停点.await运算符通知编译器异步方法: ...
- 浅读vue-router源码,了解vue-router基本原理
项目中使用vue-router的时候,会进行以下操作(可能具体不是这么写的,但是原理一样): 定义映射关系routes: 定义router实例的时候传入vue和参数{routes...}: 定义vue ...
- centos安装配置mariadb
CentOS7下使用yum安装MariaDB CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL.M ...
- javascript头像上传
上传头像: 相关关键词:ondragover(拖动元素在投放区内移动) ondrop (元素放在投放区触发但是要去处理浏览器默认事件的影响:ondragenter.ondragover) dataTr ...