【leetcode】1286. Iterator for Combination
题目如下:
Design an Iterator class, which has:
- A constructor that takes a string
characters
of sorted distinct lowercase English letters and a numbercombinationLength
as arguments.- A function next() that returns the next combination of length
combinationLength
in lexicographical order.- A function hasNext() that returns
True
if 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^4
function calls per test.- It's guaranteed that all calls of the function
next
are 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 ...
随机推荐
- php学习历程1——注册、登录(面向过程、面向对象)
首先放一张天空之城 Php入门来的第一个小项目,首先做的是一个简陋的文章管理系统.有登录.注册.文章list.添加文章.修改文章.删除文章.分页这几个小功能. 面向过程的编码 面向对象的编码 首先做的 ...
- spring boot 2.0 提示 No primary or default constructor found for interface Pageable 解决办法
在SpringBoot 2.0 以前,我们会配置以下类 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter ...
- python线程间通信
#!/usr/bin/python # -*- coding:utf8 -*- from threading import Thread, Lock import random def test_th ...
- go 拼接sql
//原文链接:https://www.jianshu.com/p/a0569157c418 golang mysql拼接子查询 使用fmt.Sprintf拼接SQL 实例代码 func Select( ...
- 14-Perl 引用
1.Perl 引用引用就是指针.Perl 引用是一个标量类型,可以指向变量.数组.哈希表(也叫关联数组)甚至子程序,可以应用在程序的任何地方.2.创建引用定义变量的时候,在变量名前面加个\,就得到了这 ...
- C#面向对象22 委托事件反射
1.委托的定义:声明委托类型(返回值和参数,命名空间中):定义委托对象 (把委托想象成函数中的占位符~因为你并不确定调用哪个函数~) using System; using System.Collec ...
- python之数字类型小知识
数字是表示计数的抽象事物,也是数学运算和推理的基础,所以,生活中数字是生活中无处不在的,那么,在python语言中运用数字有哪些小知识呢,不妨花点时间看一下这篇博文,牢记这些小知识. 整数类型中四种进 ...
- centos7安装nginx服务
Nginx发音引擎x是一个免费的开源高性能HTTP和反向代理服务器,负责处理互联网上一些最大的网站的负载. 本教程将教你如何在你的CentOS Linux 7.5机器上安装和管理Nginx. 安装Ng ...
- ext grid添加2行topbar
bbar: paginToolbar(this.getStore()), dockedItems: [{ xtype: 'toolbar', dock: 'top', items: me.create ...
- vue进阶:vue-router之导航守卫、路由元信息、路由懒加载
1.导航被触发 2.在失活的组件里调用离开守卫:beforeRouteLeave —— 组件内守卫(离开组件). 3.调用全局的beforeEach守卫 —— 全局守卫(进入组件). 4.在重用组件里 ...