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. 1 <= N <= 9
  2. 0 <= K <= 9

搜索题。两边搜索。注意N=1的特别情况,这个时候都行。

class Solution {
private:
unordered_map<int,vector<int>> mp ;
public:
vector<int> numsSameConsecDiff(int N, int K) { if(N == ){
vector<int> ret;
for(int i=; i<; i++) ret.push_back(i);
return ret;
}
for(int i=; i<; i++){
int idx = i + K;
if(idx < ) mp[i].push_back(idx);
idx = i - K;
if(!mp[i].empty() && mp[i][] == idx) continue;// K等于1特殊情况
if(idx >= ) mp[i].push_back(idx);
}
vector<int> ret;
for(int i=; i<; i++){
if(!mp.count(i)) continue;
helper(ret, N, i, );
}
return ret;
}
void helper(vector<int>& ret, int N, int start, int tmpval){
int tt = tmpval* + start;
if(to_string(tt).size() == N) {
ret.push_back(tt);
return;
}
if(!mp.count(start)) return ;
vector<int> choices = mp[start];
for(auto x : choices){
helper(ret, N, x, tt);
}
}
};

再贴一个yubowen大佬的解法

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII; #define REP(i,s,t) for(int i=(s);i<(t);i++)
#define FILL(x,v) memset(x,v,sizeof(x)) const int INF = (int)1E9;
#define MAXN 100005 class Solution {
public:
int N, K;
void solve(int i, int ld, int val, VI &ans) {
if (i == N) {
ans.push_back(val);
return;
}
REP(t,,) {
int d = t == ? ld + K : ld - K;
if (K == && t == ) continue;
if ( <= d && d <= ) {
solve(i+, d, val* + d, ans);
}
}
}
vector<int> numsSameConsecDiff(int _N, int _K) {
N = _N; K = _K;
VI ans;
if (N == ) {
REP(i,,) ans.push_back(i);
return ans;
}
REP(s,,) solve(, s, s, ans);
return ans;
}
};

LC 967. Numbers With Same Consecutive Differences的更多相关文章

  1. 【leetcode】967. Numbers With Same Consecutive Differences

    题目如下: Return all non-negative integers of length N such that the absolute difference between every t ...

  2. 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  3. [Swift]LeetCode967. 连续差相同的数字 | Numbers With Same Consecutive Differences

    Return all non-negative integers of length N such that the absolute difference between every two con ...

  4. LC 562. Longest Line of Consecutive One in Matrix

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  5. [LC] 659. Split Array into Consecutive Subsequences

    Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or ...

  6. [LC] 298. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. LeetCode Weekly Contest 117

    已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...

  8. Weekly Contest 117

    965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. ...

  9. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

随机推荐

  1. python之初入Python

    python优缺点: Python的优点很多,简单的可以总结为以下几点. 简单和明确,做一件事只有一种方法. 学习曲线低,跟其他很多语言相比,Python更容易上手. 开放源代码,拥有强大的社区和生态 ...

  2. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  3. uboot驱动模型(DM)分析(二) (转)

    上篇分析了两个关键宏U_BOOT_DRIVER及U_BOOT_DEVICES的作用,有了上篇的基础,本文将分析: 1.上篇中的uboot_list段中的信息如何被用起来? 2.uclass,uclas ...

  4. Alpha版本

    这个作业属于哪个课程 [课程链接][ https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/] 这个作业要求在哪里 [作业要求][ ...

  5. puppet 3+Unicorn+Nginx安装配置

    puppet 3+Unicorn+Nginx安装配置 2014-08-15 10:58 酒瓶不倒 酒瓶不倒的博客 字号:T | T 一键收藏,随时查看,分享好友! Unicorn 效率要比 Webri ...

  6. LocalDate使用(转)

    3.Date 3.1.JDK7 Date缺点 1.所有的日期类都是可变的,因此他们都不是线程安全的,这是Java日期类最大的问题之一 2.Java的日期/时间类的定义并不一致,在java.util和j ...

  7. Codeforces 871C 872E Points, Lines and Ready-made Titles

    题 OvO http://codeforces.com/contest/871/problem/C ( Codeforces Round #440 (Div. 1, based on Technocu ...

  8. 彻底卸载干净docker并且安装docker 指定版本

    yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-la ...

  9. node的http与前端交互示例(入门)

    一.目录(node_modules是npm install后新增的) node 和 npm 版本 npm install http 二.node下的index.js var http = requir ...

  10. Vue 工程化最佳实践

    目录结构 总览   api 目录用于存放 api 请求,文件名与模型名称基本一致,文件名使用小驼峰,方法名称与后端 restful 控制器一致.   enums 目录存放 常量,与后端的常量目录对应 ...