【leetcode】967. Numbers With Same Consecutive Differences
2024-09-06 05:40:07
题目如下:
Return all non-negative integers of length
N
such that the absolute difference between every two consecutive digits isK
.Note that every number in the answer must not have leading zeros except for the number
0
itself. For example,01
has one leading zero and is invalid, but0
is valid.You may return the answer in any order.
Example 1:
Input: N = 3, K = 7
Output: [181,292,707,818,929]
Explanation: Note that 070 is not a valid number, because it has leading zeroes.Example 2:
Input: N = 2, K = 1
Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]Note:
1 <= N <= 9
0 <= K <= 9
解题思路:题目本身很简单,利用BFS/DFS都可以。但是有两点要注意的,一是K = 0 的情况会造成重复(+0和-0的值一样),二是N等于1的时候,别忘了0也是其中一个结果。
解题思路:
class Solution(object):
def numsSameConsecDiff(self, N, K):
"""
:type N: int
:type K: int
:rtype: List[int]
"""
res = []
for i in range(1,10):
queue = [str(i)]
while len(queue) > 0:
item = queue.pop(0)
if len(item) == N:
res.append(item)
continue
last = int(item[-1])
if (last - K) >= 0:
queue.append(item + str(last - K))
if K != 0 and abs(last + K) < 10:
queue.append(item + str(last + K))
if N == 1:
res.insert(0,'')
return [int(i) for i in res]
最新文章
- Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au
- nginx 相关问题
- DefaultHttpClient使用
- String、Brush、Color 相互转换
- Redis 该选择哪种持久化配置
- Python学习笔记6(列表生成式)
- 卡特兰数 Catalan数 ( ACM 数论 组合 )
- angular2与VS2015 asp.net core集成使用
- Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件
- MySQL/MariaDB中游标的使用
- Spring学习笔记5——注解方式AOP
- elasticsearch -- kibana安装配置
- Windows下的包管理工具-Scoop
- Hadoop学习之路(二十三)MapReduce中的shuffle详解
- 2017-2018-2 20155303『网络对抗技术』Exp8:Web基础
- windows环境下把Python代码打包成独立执行的exe
- Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则
- c# 中实用包,实用dll。
- Kali信息收集-搜索引擎
- Python在线dlib库地址