当前位置:网站首页>模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
2022-07-06 06:15:00 【邂逅模拟卷】
1296. 划分数组为连续数字的集合
给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合。
如果可以,请返回 true;否则,返回 false。
示例 1:
输入:nums = [1,2,3,3,4,4,5,6], k = 4
输出:true
解释:数组可以分成 [1,2,3,4] 和 [3,4,5,6]。
示例 2:
输入:nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
输出:true
解释:数组可以分成 [1,2,3] , [2,3,4] , [3,4,5] 和 [9,10,11]。
示例 3:
输入:nums = [3,3,2,2,1,1], k = 3
输出:true
示例 4:
输入:nums = [1,2,3,4], k = 3
输出:false
解释:数组不能分成几个大小为 3 的子数组。
提示:
1 <= k <= nums.length <= 105
1 <= nums[i] <= 109
注意:此题目与 846 重复:https://leetcode-cn.com/problems/hand-of-straights/
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums)%k>0:return False
cnt = Counter(nums)
while cnt:
x = min(cnt)
for n in range(x,x+k):
if cnt[n]==1:cnt.pop(n)
elif cnt[n]>1:cnt[n]-=1
else:return False
return True
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- The latest 2022 review of "graph classification research"
- LeetCode 732. 我的日程安排表 III
- JWT-JSON WEB TOKEN
- 2022 software testing workflow to know
- Application of Lie group in gtsam
- 10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- Idea new UI usage
- Amazon Engineer: eight important experiences I learned in my career
- MySQL之数据类型
猜你喜欢
随机推荐
【Postman】Collections-运行配置之导入数据文件
单元测试的意义
Leaflet map
selenium源码通读·9 |DesiredCapabilities类分析
F - True Liars (种类并查集+DP)
LeetCode 731. 我的日程安排表 II
P问题、NP问题、NPC问题、NP-hard问题详解
Customize the gateway filter factory on the specified route
Hypothesis testing learning notes
职场进阶指南:大厂人必看书籍推荐
2022 software testing workflow to know
Construction and integration of Zipkin and sleuth for call chain monitoring
浅谈专项测试之弱网络测试
数学三大核心领域概述:几何
自定义指定路由上的Gateway过滤器工厂
技术分享 | 常见接口协议解析
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
Resttemplate and feign realize token transmission
一文揭开,测试外包公司的真 相
Commodity price visualization



![[postman] collections - run the imported data file of the configuration](/img/85/7ac9976fb09c465c88f376b2446517.png)





