【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/numbers-with-same-consecutive-differences/
题目描述
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 <= N <= 9
- 0 <= K <= 9
题目大意
找出N位数中,所有满足每个数字的所有连续数相减绝对值等于K的数字。比如第一个例子的181就满足|8-1| = |1 - 8| = 7.
解题方法
DFS
明显这是个找出所有符合条件的题目,因此是个搜索题。看了给出的数字的范围只有9位数,大概判断使用DFS不会超时。因此,我们使用DFS找出所有符合条件的即可。
这里的DFS搜索方法是,我们先确定首位数字是1到9,然后计算以这个数字开头的整数满足条件的有多少。也就是末位数字 + K <= 9
或者末位数字 + K >= 0
两种符合条件,可以继续向后搜索,知道搜索到N==0,那么搜索结束,把现在的整数放到结果里即可。
题目里面有两个坑:第一,先导0的问题,我在上面搜索的过程中是假设了第一位数字不是0了,那么对于N>=2的时候是满足的,当N==1的时候直接返回0~9各个数字即可,这点题目没有说清楚,我觉得是不好的。第二,题目没有专门提到返回的数字不能有重复,我觉得题目应该提醒一下。
python代码如下:
class Solution(object):
def numsSameConsecDiff(self, N, K):
"""
:type N: int
:type K: int
:rtype: List[int]
"""
if N == 1:
return [0, 1,2,3,4,5,6,7,8,9]
res = []
for i in range(1, 10):
self.dfs(res, i, N - 1, K)
return list(set(res))
def dfs(self, res, curint, N, K):
if N == 0:
res.append(curint)
return
last = curint % 10
if last + K <= 9:
self.dfs(res, curint * 10 + last + K, N - 1, K)
if last - K >= 0:
self.dfs(res, curint * 10 + last - K, N - 1, K)
用C++再写了一遍的时候,对去重的处理时当K不等于0的时候再向更小的数字搜索,因为K等于0的搜索已经在last + K <=9中完成了。C++代码如下:
class Solution {
public:
vector<int> numsSameConsecDiff(int N, int K) {
if (N == 1)
return {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector<int> res;
for (int i = 1; i <= 9; i++)
helper(res, i, N - 1, K);
return res;
}
void helper(vector<int>& res, int curint, int N, int K) {
if (N == 0) {
res.push_back(curint);
return;
}
int last = curint % 10;
if (last + K <= 9)
helper(res, curint * 10 + last + K, N - 1, K);
if (last - K >= 0 && K)
helper(res, curint * 10 + last - K, N - 1, K);
}
};
日期
2018 年 12 月 30 日 —— 周赛差强人意
【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)的更多相关文章
- 【leetcode】967. Numbers With Same Consecutive Differences
题目如下: Return all non-negative integers of length N such that the absolute difference between every t ...
- LC 967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
随机推荐
- Linux非root安装Python3以及解决SSL问题
说明 接上一篇. [Linux]非root安装Python3及其包管理 上一篇虽然成功安装了Python3及一些常用的模块,但因为一直装不上SSL模块,导致一些包无法安装,尝试了不少方法都失败了(网上 ...
- Python压缩&解压缩
Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...
- shell 脚本的基本定义
注意不能有控制,指令之间 [1]shell脚本的基础知识 (1)shell脚本的本质 编译型语言 解释型语言 shell脚本语言是解释型语言 shell脚本的本质 shell命令的有序集合 (2)sh ...
- ChromeDriver的安装和使用
用于驱动Chrome浏览器,适用于有界面的操作系统. 一.安装ChromeDriver 要先安装Chrome浏览器,然后安装ChromeDriver. 官方网站:https://sites.googl ...
- Demo02一千以内的水仙花数
package 习题集2;//1000以内的水仙花数public class Demo02 { public static void main(String[] args) { int i = 100 ...
- HTML5 基础内容(元素/属性/格式化)
HTML基础 1.HTML元素 1.1 元素指的是开始标签到结束标签的所有代码. 1.2 元素的内容是开始标签与结束标签之间的内容. 1.3大多数HTML元素可用有属性. 1.4标签可以拥有属性为元素 ...
- Oracle参数文件—pfile与spfile
oracle的参数文件:pfile和spfile 1.pfile和spfile Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动时候加载的, ...
- KMP算法中的next函数
原文链接:http://blog.csdn.net/joylnwang/article/details/6778316/ 其实后面大段的代码都可以不看 KMP的关键是next的产生 这里使用了中间变量 ...
- mysql删除数据后不释放空间问题
如果表的引擎是InnoDB,Delete From 结果后是不会腾出被删除的记录(存储)空间的. 需要执行:optimize table 表名; eg:optimize table eh_user_b ...
- MFC入门示例之树控件(CTreeControl)
1 //增加按钮 2 void CMFCApplication8Dlg::OnBnClickedButtonAdd() 3 { 4 //树中添加节点 5 CString strText; 6 GetD ...