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 <= 90 <= 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 ...
随机推荐
- Delphi Android拍照报错
打开拍照提示以上错误,解决方式
- kali安装开启ssh & 安装docker
ssh相关 一.配置SSH参数 . 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将N ...
- python中F/f表达式优于format()表达式
F/f表达式可以解析任意类型的数据 具体实现,看下面示例: 1.解析变量 1 a = 10 3 b = 20 5 res1 = F"a+b的值:{a+b}" 7 print(res ...
- shell基本概念
一.shell分类 常见shell:bash.zsh.tcsh linux默认:bash shell #tcsh #bash #pstree | grep login |- .. .. |-login ...
- 浅谈ROW_NUMBER() OVER()函数的使用
语法格式:row_number() over(partition by 分组列 order by 排序列 desc) row_number() over 具有分组排 ...
- QTP(6)
一.检查点 1.标准检查点(Standard Checkpoint) 作用:检查对象的属性值是否正确 文本框对象:text 预期值 单选按钮对象:checked ON/OFF 下拉框对象:Select ...
- PHP通过php-java-bridge调用JAVA的jar包里class类
正 文: 有的时候我们需要在PHP里调用JAVA平台封装好的jar包里的class类和方法,一般飘易推荐的做法是采用php-java-bridge做桥接,本文就来介绍一下大致的实现方法. 先简单说 ...
- Java的日期与时间 java.time.Duration (转)
一个Duration对象表示两个Instant间的一段时间,是在Java 8中加入的新功能. 一个Duration实例是不可变的,当创建出对象后就不能改变它的值了.你只能通过Duration的计算方法 ...
- linux下安装虚拟环境
安装pip $ wget https://bootstrap.pypa.io/get-pip.py $ python get-pip.pypython3 -m pip install --upgrad ...
- 9030PCI CAN驱动开发点滴
1.配置EEPROM. 使用PlxMon打开9030,基本修改Spacex(0,1,2,3), Chip selectx(0,1,2,3), 中断状态INTCSR(0x74), 其他(0x78),详细 ...