【leetcode】967. Numbers With Same Consecutive Differences
题目如下:
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]
【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 ...
随机推荐
- jQuery实现网页放大镜功能 转载
京东等电商网站中可以对商品进行放大观察,本文要实现的就是模仿这个放大镜功能,大致效果如下图所示: 简要说明实现思路: 1.原图窗口与放大窗口插入的是同一个图片,不过原图窗口的图片要适当缩小,放大窗口图 ...
- 第07章 JdbcTemplate
第07章JdbcTemplate 1. 概述 为了使JDBC更加易于使用,Spring在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架. 作为Spring JDBC框架的核心,JDB ...
- sql格式化时间
sql格式化date类型 DATE_FORMAT(nuw(), '%Y-%m-%d') sql格式化long类型时间 FROM_UNIXTIME(time/1000,'%Y-%m-%d')
- C盘Administrator中 .m2/repository里面是什么
${user.home}/.m2/repository文件夹是maven默认的本地仓库地址maven仓库分为远程仓库和本地仓库,当你在pom里配置依赖项目后,maven首先会从本地仓库查找该项目,如果 ...
- Zip函数(Python)
>>> z = zip((2,3,4),(33,44,55)) >>> z <zip object at 0x1022cdb88> >>&g ...
- PHP disk_free_space() 函数
定义和用法 disk_free_space() 函数返回指定目录的可用空间,以字节为单位. 语法 disk_free_space(directory) 参数 描述 directory 必需.规定要检查 ...
- 二进制&八进制&十六进制之间的快速转换------ 心算&笔算方法总结
二进制数 0&1两种元素: 8进制数 0-7 八种元素: 十六进制数 0-9,a,b,c,d,e, ...
- JMeter ServerAgent服务器资源监控插件
本文介绍对Linux服务器的服务进行压测时,使用jmeter serverAgent插件监控服务器资源. 1.插件准备 所需插件: JMeterPlugins-Extras.jar JMeterPlu ...
- ecshop整合discuz教程完美教程
所需软件: ecshop安装包: ECShop_V2.7.3_UTF8_release1106.rarucenter安装包: UCenter_1.6.0_SC_UTF8.zipdiscuz! ...
- Gym101158 J 三分 or 模拟退火 Cover the Polygon with Your Disk
目录 Gym101158 J: 求圆与给定凸多边形最大面积交 模拟退火 三分套三分 模拟退火套路 @ Gym101158 J: 求圆与给定凸多边形最大面积交 传送门:点我点我 求 $10 $ 个点组成 ...