【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 ...
随机推荐
- IO(上)
1 输入流和输出流 输入流,数据从源数据源流入程序的过程称为输入流.可以理解为从源数据源读取数据到程序的过程. 输出流,数据从程序流出到目的地的过程称为输出流.可以理解为把数据从程序写入目的地的过程. ...
- Forest Program(dfs方法---树上的环)
题意:http://acm.hdu.edu.cn/showproblem.php?pid=6736 沙漠中的每一个连通块都是一棵仙人掌:一个连通块是一棵仙人掌当且仅当连通块中不存在重边和自环,并且每一 ...
- ThinkPHP composer的安装,及image图像处理类库的加载
以下教程针对windows系统,示例系统使用win7 composer安装 下载composer安装包,点击安装. 出现'composer choose the command-line php' 要 ...
- vue配置外放generate-asset-webpack-plugin
解决方法:(共有2个方法) 1.借助插件 generate-asset-webpack-plugin .在webpack.prod.conf.js中去生成configServer.json文件,让其 ...
- weblogic连接池
1.在 使用JDBC连接池的过程中,最常见的一个问题就是连接池泄漏问题.一个池里面的资源是有限的,应用用完之后应该还回到池中,否则池中的资源会被耗尽. WebLogic Server提供了一个Inac ...
- JS基础_break和continue
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Nginx作为代理服务之反向代理
Nginx作为代理服务之反向代理 需求:我们需要访问一个服务,但是服务端只接受8080端口,所以需要在nginx中配置反向代理,帮助客户端代理实现. 1. 创建一个html放入到一个文件夹中 2. 在 ...
- & 位运算总结
一.& 与 a & -a : 可以计算出 a 的二进制形式的第一个 1 出现的位置. eg: 6 & -6 = 0110 & 1010 = 0010
- 【Day1】1.了解Python
视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...
- 静态static最基础的知识
static静态: 常见修饰的内容: 1.变量: 修饰变量时,叫静态变量或类变量.此变量为类所有随着虚拟机加载类是而加载入方法区,此静态变量为该类所有对象共享,在内存中只有一个副本,它 当且仅当 类的 ...