【leetcode】967. Numbers With Same Consecutive Differences
题目如下:
Return all non-negative integers of length
Nsuch 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
0itself. For example,01has one leading zero and is invalid, but0is 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 <= 90 <= 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的更多相关文章
- 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
- 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...
- 【leetcode】1012. Numbers With Repeated Digits
题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...
- 【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 ...
- 【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 ...
- 【leetcode】659. Split Array into Consecutive Subsequences
题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...
- LC 967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
随机推荐
- idea spring boot搭建笔记
如何建立spring boot项目 首先建一个empty Probject,Modules导入新创建的Project new modules选择Spring lnitializr ->next( ...
- github 配置ssh
https://www.cnblogs.com/superGG1990/p/6844952.html
- C#中using语句是什么意思
使用using语句最终生成的其实是一个try, finally代码块,在finally代码块里释放资源.要求是:为 using 语句提供的对象必须实现 IDisposable 接口.此接口提供了 Di ...
- http协议和file协议的区别
1.在本地直接使用浏览器打开 html文件 和 通过本地服务器打开 html文件 有什么区别呢. https://segmentfault.com/q/1010000006554881/a-1 ...
- Golang 开发技能图谱
# Golang 开发技能图谱 ## Go 语言环境搭建- Go 安装- GOPATH 与工作空间- Go 命令- Go开发工具 ## GO 语言编程基础- 关键词和语法(Language Synta ...
- 63、saleforce DML
PRIVELEGE__c privilege = new PRIVELEGE__c(PRIVILAGENAME__c = '权限添加',PRIVILAGEDESCRIBE__c = '权限描述'); ...
- python学习笔记:json与字典的转换(dump 和dumps load和loads的区别)
1. json序列化(字典转成字符串)方法: dumps:无文件操作 dump:序列化+写入文件 2. json反序列化(字符串转成字典)方法: loads:无文件操作 ...
- Java方法覆盖教程
重新定义来自超类(父类)继承的类中的实例方法称为方法覆盖. 示例 现在来看看,类A和类B的以下声明,覆盖了 print() 方法 : 1 2 3 4 5 6 7 8 9 10 11 public cl ...
- Maven远程仓库地址
https://repo1.maven.org/maven2/ http://maven.jahia.org/maven2/ http://maven.aliyun.com/nexus/content ...
- leetcode.数组.565数组嵌套-Java
1. 具体题目 索引从0开始长度为N的数组A,包含0到N - 1的所有整数.找到并返回最大的集合S,S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }且遵守以下的规则.假 ...