【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 ...
随机推荐
- 【串线篇】SpringMvc源码分析
一.DispathcherServlet结构分析 1).所有请求过来DispatcherServlet收到请求, 2).调用doDispatch()方法进行处理 1).getHandler():根据当 ...
- zoj 2112 单点修改的主席树(树状数组套主席树)
题目大意: 区间第k大问题+单点修改 基本思路: 这个题有用整体二分,cdq分治,还有主席树+平衡树的,还有就是主席树+树状数组. 我采用的是b站电子科大大佬的主席树写法,尤其喜欢他的离散化方法,所以 ...
- cerebro使用
一.安装cerebro 上传插件到任意路径 tar zxvf 解压插件包 [root@ngsocdev14 es]# ls cerebro-0.8.3.zip software [root@ngsoc ...
- HTML中多媒体标签技术说明
在纯文本的HTML页面中加入图片,给原来单调乏味的页面添加生气.HTML语言中利用<IMG>标记插入图片. 1.图片标记<IMG>及其属性 在网站上,网页设计者都使用了大量精心 ...
- PHP的Session机制解析 1
一.php的默认session机制 php默认用磁盘文件来实现session在php.ini中session.save_handler = files定义session机制session.save_p ...
- kafka ConsumerConfig 配置
- 运维 04 Shell基础命令(二)
Shell基础命令(二) 查看Linux的发行版 cat /etc/redhat-release cat /etc/os-release 查看系统用户的id信息 id 用户名 id root id ...
- 简单了解Redis
redis是什么 redis是一种支持key-value等多种数据结构的存储系统,可用于缓存,事件发布,消息队列等场景,支持多种数据类型 string.hash.list.set.zset.而且基于内 ...
- SpringBoot扫描不到类,注入失败A component required a bean of type 'XXService' that could...
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类.这个类的位置很关键: 如果App ...
- Scrapy框架: 基本命令
1.创建爬虫项目 scrapy startproject [项目名称] 2.创建爬虫文件 scrapy genspider +文件名+网址 3.运行(crawl) scrapy crawl 爬虫名称 ...