LC 967. 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=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的更多相关文章
- 【leetcode】967. Numbers With Same Consecutive Differences
题目如下: Return all non-negative integers of length N such that the absolute difference between every t ...
- 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- [Swift]LeetCode967. 连续差相同的数字 | Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode Weekly Contest 117
已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...
- Weekly Contest 117
965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
随机推荐
- Java学习笔记【七、时间、日期、数字】
参考:http://www.runoob.com/java/java-date-time.html Date类 构造: Date() 使用当前的日期时间 Date(long millisec) 197 ...
- python入坑级
pycharm设置 pycharm设置自动换行的方法 只对当前文件有效的操作:菜单栏->View -> Active Editor -> Use Soft Wraps: 如果想对所有 ...
- 03:Java基础语法(二)
Java基础语法 Java运算符 算术运算符 运算符是一种特殊的符号,用以表示数据的运算.赋值和比较等.1.操作数:参与运算的数据 称为操作数.2.表达式:运算符和操作数的整体 称为表达式.单独的一个 ...
- 8.7.ZooKeeper Watcher监听
1.ZooKeeper Watcher ZooKeeper 提供了分布式数据发布/订阅功能,一个典型的发布/订阅模型系统定义了一种一对多的订阅关系,能让多个订阅者同时监听某一个主题对象, 当这个主题对 ...
- Ubuntu 18.04 安装配置 MySQL 5.7
Ubuntu 18.04 安装 mysql 的过程中,竟然没有让你输入秘密?!(之前在 Ubuntu 14.04 下,安装过程中会询问密码),这导致安装完 mysql 初始秘密不知道的问题. $ su ...
- Cookie/Session的机制
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- H265码流格式
一.H265码流格式 VPS:视频参数集,用于传输视频分级信息,有利于兼容标准在可分级视频编码或多视点视频的扩展. NALU header定义: NALU header(){ Descriptor f ...
- 《Python基础教程》第一章:基础知识
如果希望只执行普通的除法,可以在程序前加上以下语句:from __future__ import division.还有另外一个方法,如果通过命令行运行Python, 可以使用命令开关-Qnew.此时 ...
- java 学习笔记(三)ZooKeeper集群搭建实例,以及集成dubbo时的配置 (转)
ZooKeeper集群搭建实例,以及集成dubbo时的配置 zookeeper是什么: Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式 ...
- 【wqs二分 || 决策单调性】cf321E. Ciel and Gondolas
把状态看成层,每层决策单调性处理 题目描述 题目大意 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半.这不?他们遇到了大麻烦!n只贞鱼到陆地上乘车,现在有k辆汽车可以租用.由于贞鱼们 ...