题目如下:

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]

【leetcode】967. Numbers With Same Consecutive Differences的更多相关文章

  1. 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  2. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  3. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

  4. 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...

  5. 【leetcode】1012. Numbers With Repeated Digits

    题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...

  6. 【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List

    题目如下: Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum ...

  7. 【leetcode】1033. Moving Stones Until Consecutive

    题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...

  8. 【leetcode】659. Split Array into Consecutive Subsequences

    题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...

  9. LC 967. Numbers With Same Consecutive Differences

    Return all non-negative integers of length N such that the absolute difference between every two con ...

随机推荐

  1. java输出当前文件所在路径

    System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径

  2. 一张图告诉你js为什么要加分号

    当js代码被压缩或者通过其他方式改变你的编码结构时,分号能够给编译器和解析器提供精准的语句拆分. 如图中m 和 c 的例子就能解释为什么这样做.

  3. Python--前端基础之JavaScript(JS的引入方式,JS的变量、常量和标识符,JS的数据类型,运算符,流程控制,JavaScript的对象)

    JavaScript介绍 JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互. JavaScript是浏览器解释执行的,前端脚 ...

  4. Python--MySql(主键的创建方式、存储引擎、存储过程、索引、pymsql)

    主键的创建方式 1. create table stud( id int not null unique, name ) ); mysql> desc stud; +-------+------ ...

  5. GO 学习资源收集

     golang图书,在线阅读Go轻松学https://www.golang123.com/book/16 Go示例学https://www.golang123.com/book/17 Go Web 编 ...

  6. [CSP-S模拟测试]:队长快跑(DP+离散化+线段树)

    题目背景 传说中,在远古时代,巨龙大$Y$将$P$国的镇国之宝窃走并藏在了其巢穴中,这吸引着整个$P$国的所有冒险家前去夺回,尤其是皇家卫士队的队长小$W$.在$P$国量子科技实验室的帮助下,队长小$ ...

  7. Effective Objective-C 2.0

    Effective Objective-C 2.0:编写高质量iOS与OS X代码的52个有效方法 作者:Matt Galloway(英) 译者:爱飞翔 出版社:机械工业出版社 出版年:2014-01 ...

  8. Nginx + Tomcat 配置负载均衡集群简单实例

    一.Hello world 1.前期环境准备 准备两个解压版tomcat,如何同时启动两个tomcat,请看我的另一篇文章<一台机器同时启动多个tomcat>. nginx官网下载解压版n ...

  9. Nginx功能模块汇总

    主要文档 Nginx功能概述.为什么选择Nginx.Nginx安装.常见问题(FAQ).配置符号参考.调试 nginx.优化 Nginx.运行和控制Nginx 核心模块 Nginx事件模块.Nginx ...

  10. (2)C++基本类型

    一.整形 short .int. long. long long sizeof查看类型所占的字节数 cout << sizeof(short)<<endl;//2 字节 cou ...