【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 ...
随机推荐
- Springmvc使用注解实现执行定时任务(定时器)
1.在Spring配置文件中添加 <task:annotation-driven/> 2.在需要调用执行定时任务的类中使用注解 @Service @Lazy(false) //避免spri ...
- python使用xlutils库save()之后,文件损坏
import xlrd from xlutils.copy import copyworkbook=xlrd.open_workbook('test.xlsx')##打开excel为 .xlsx格式w ...
- DRF cbv源码分析 restful规范10条 drf:APIView的源码 Request的源码 postman的安装和使用
CBV 执行流程 路由配置:url(r'^test/',views.Test.as_view()), --> 根据路由匹配,一旦成功,会执行后面函数(request) --> 本质就是执 ...
- flask数据库迁移
实际操作顺序:1.python 文件 db init2.python 文件 db migrate -m"版本名(注释)"3.python 文件 db upgrade 然后观察表结构 ...
- Java Web ActiveMQ与WebService的异同
Webservice 和MQ(MessageQueue)都是解决跨平台通信的常用手段 一.WebService:用来远程调用服务,达到打通系统.服务复用的目的.是SOA系统架构——面向服务架构的体现. ...
- mybatis 主键自增异常
org.springframework.jdbc.UncategorizedSQLException: Error getting generated key or setting result t ...
- 作业8:常用java命令(二)
一.jinfo(Configuration Info for Java) 1.功能:jinfo可以实时地查看和调整虚拟机的各项参数. 2.参数: 选项 作用 -flag name 打印改名字的VM设置 ...
- [转载]VS2005的工程用VS2010打开后,用VS2005不能打开的解决方法
首先,在“项目”菜单里,把项目属性“目标平台”改为框架2.0,保存退出. 然后,用记事本或用编辑文本文件的方式打开你的项目文件,后缀为.sln 第一行:把“Microsoft Visual Studi ...
- jquery选择器 模糊查找
$("input[class^='combo-text']").attr("readonly", "readonly"); 查找包含‘com ...
- Bootstrap页面响应式设计
关键词:viewport.栅格布局.媒体查询(Media Queries) 一.关于栅格布局的说明: 1.基本图解 extra small devices phones 超小型设备手机small d ...