题目如下:

Return all non-negative integers of length N such that the absolute difference between every two consecutive digits is K.

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, but 0 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. 1 <= N <= 9
  2. 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]

最新文章

  1. Atian inputmethod 输入法解决方案 方言与多语言多文字支持 英语汉字汉语阿拉伯文的支持 (au
  2. nginx 相关问题
  3. DefaultHttpClient使用
  4. String、Brush、Color 相互转换
  5. Redis 该选择哪种持久化配置
  6. Python学习笔记6(列表生成式)
  7. 卡特兰数 Catalan数 ( ACM 数论 组合 )
  8. angular2与VS2015 asp.net core集成使用
  9. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件
  10. MySQL/MariaDB中游标的使用
  11. Spring学习笔记5——注解方式AOP
  12. elasticsearch -- kibana安装配置
  13. Windows下的包管理工具-Scoop
  14. Hadoop学习之路(二十三)MapReduce中的shuffle详解
  15. 2017-2018-2 20155303『网络对抗技术』Exp8:Web基础
  16. windows环境下把Python代码打包成独立执行的exe
  17. Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则
  18. c# 中实用包,实用dll。
  19. Kali信息收集-搜索引擎
  20. Python在线dlib库地址

热门文章

  1. 常用的外网yum源之epel.repo
  2. PC-lint初体验
  3. window杀死端口
  4. pycharm查找替换快捷键
  5. pc上用C语言模拟51多任务的案例程序
  6. 阿里云入选Gartner 2019 WAF魔力象限,唯一亚太厂商!
  7. SSD接口详解,再也不会买错固态硬盘了
  8. POJ 2387 Til the Cows Come Home (dijkstra模板题)
  9. shell变量相关知识
  10. Dubbo入门到精通学习笔记(十八):使用Redis3.0集群实现Tomcat集群的Session共享