【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- ...
随机推荐
- quota
一.什么是磁盘配额 磁盘配额从字面意思上看就是给一个磁盘配置多少额度,而quota就是有多少限额的意思,所以总的来说就是限制用户对磁盘空间的使用量.因为Linux是多用户多任务的操作系统,许多人公用磁 ...
- shell编程100列
1.编写hello world脚本 #!/bin/bash# 编写hello world脚本 echo "Hello World!"2.通过位置变量创建 Linux 系统账户及密码 ...
- lua_newthread的真正意义
lua_newthread 这个接口,存在误导性,很多人第一次试图用它来解决多线程问题时,都会入坑. 实际上,这个接口真正的用法,是给那些在lua更底层的某些行为(通常是递归)导致了lua的栈溢出而准 ...
- 【系统硬件】英伟达安培卡 vs 老推理卡硬件参数对比
欢迎关注我的公众号 [极智视界],回复001获取Google编程规范 O_o >_< o_O O_o ~_~ o_O 本文分享一下英伟达安培卡 vs 老推理 ...
- Learning Spark中文版--第三章--RDD编程(1)
本章介绍了Spark用于数据处理的核心抽象概念,具有弹性的分布式数据集(RDD).一个RDD仅仅是一个分布式的元素集合.在Spark中,所有工作都表示为创建新的RDDs.转换现有的RDD,或者调 ...
- 如何通过 User-Agent 识别百度蜘蛛
如果有大量的百度蜘蛛抓取网站就需要注意了:有可能是其他爬虫伪造百度蜘蛛恶意抓取网站. 如果遇到这种情况,这时候就需要查看日志来确定是不是真正的百度蜘蛛(baidu spider).搜索引擎蜘蛛.用户访 ...
- Gradle插件详解
参考[1]Gradle 插件 [2]修改 Gradle 插件(Plugins)的下载地址(repositories)
- 用oracle中的Row_Number实现分页
Row_Number实现分页 1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号 ...
- 解决git push报错error: failed to push some refs to 的问题
这个问题发生的背景一般是: 想把自己本地的某个项目关联到远程仓库并推送上去,接着他会做如下操作: 本地项目->远程创建仓库->本地关联远程->推送最新代码 最后一个步骤发生问题: 那 ...
- 【Linux】【RedHat】下载 安装 注册
RedHat 下载 安装 注册 记录 因为找入口太麻烦了,所以写了篇博文记录下来大致入口@萌狼蓝天 注册 点击进入注册地址(https://www.redhat.com/wapps/ugc/regis ...