LeetCode 1215. Stepping Numbers
原题链接在这里:https://leetcode.com/problems/stepping-numbers/
题目:
A Stepping Number is an integer such that all of its adjacent digits have an absolute difference of exactly 1
. For example, 321
is a Stepping Number while 421
is not.
Given two integers low
and high
, find and return a sorted list of all the Stepping Numbers in the range [low, high]
inclusive.
Example 1:
Input: low = 0, high = 21
Output: [0,1,2,3,4,5,6,7,8,9,10,12,21]
Constraints:
0 <= low <= high <= 2 * 10^9
题解:
The candidate stepping numbers starting from 1, 2, 3...9.
If the current stepping number is 1, the generated ones based on it could 10 or 12.
Use BFS to iteate all possible candidates, if current number is within [low, high], add it to res.
Ortherwise, if it is <= high/10, in case of overflow, add its generated numbers to queue.
Corner case is 0. If low is 0, add it specifically. Because generated number 01 is not leagal.
Time Complexity: O(2^n). 9*(2^0 + 2^1 + 2^2 + ... + 2^n). n is digit number of high.
Space: O(2^n).
AC Java:
class Solution {
public List<Integer> countSteppingNumbers(int low, int high) {
List<Integer> res = new ArrayList<>();
if(low > high){
return res;
} LinkedList<Integer> que = new LinkedList<>();
for(int i = 1; i<=9; i++){
que.add(i);
} if(low == 0){
res.add(0);
} while(!que.isEmpty()){
int cur = que.poll();
if(cur >= low && cur <= high){
res.add(cur);
} if(cur <= high/10){
int lastDigit = cur%10;
if(lastDigit > 0){
que.add(cur*10 + lastDigit - 1);
} if(lastDigit < 9){
que.add(cur*10 + lastDigit + 1);
}
}
} return res;
}
}
LeetCode 1215. Stepping Numbers的更多相关文章
- 【leetcode】1215.Stepping Numbers
题目如下: A Stepping Number is an integer such that all of its adjacent digits have an absolute differen ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
随机推荐
- python自动化测试之appium环境安装
1.安装client pip install Appium-Python-Clinet 若有两个版本的python则使用(python3 -m pip install Appium-Python-C ...
- oralce学习笔记(二)
分区清理: --范围分区示例 drop table range_part_tab purge; --注意,此分区为范围分区 create table range_part_tab (id number ...
- js调用浏览器下载
$scope.Download = function (url) { var save_link = document.createElementNS("http://www.w3.org/ ...
- 修改Nodejs内置的npm默认配置路径方法
Nodejs 内置的npm默认会把模块安装在c盘的用户AppData目录下(吐槽一下:不明白为啥现在的软件都喜欢把资源装在这里) C盘这么小,肯定是不行的,下面一步步修改到D盘 1.打开cmd命令行, ...
- 关于何时执行shiro AuthorizingRealm 里的 doGetAuthenticationInfo与doGetAuthorizationInfo
1.doGetAuthenticationInfo执行时机如下 当调用Subject currentUser = SecurityUtils.getSubject(); currentUser.log ...
- MySQL Replication--半同步复制(Semi-Sync Replication)
半同步复制 默认配置下,MYSQL主从库通过binlog来保持一致,主库事务提交后,将binlog日志写入磁盘,然后返回给用户,备库通过拉取主库的binlog来同步主库的操作,无法保证主备节点数据实时 ...
- QuickStart系列:docker部署之Gitlab本地代码仓库
gitlab是可以在本地搭建的使用git作为源代码管理的仓库. 运行环境: win10+vmware14+docker7+docker 1. 使用命令拉取镜像(非必须,耗时比较久,这里以ce为准,ce ...
- Keystore was tampered with, or password was incorrect
#修改key条目密码 # keytool -keypasswd -alias test.com -keypass oldkeypwd -new newkeypwd -storepass storepw ...
- 从linux进程角度看JVM内存模型
普通进程栈区,在JVM一般仅仅用做线程栈,如下图所示 首先是永久代.永久代本质上是Java程序的代码区和数据区.Java程序中类(class),会被加载到整个区域的不同数据结构中去,包括常量池.域.方 ...
- 执行chmod -R 777 / 补救
执行后千万不要退出当前窗口!!! 在自己的虚拟机上设置某个站的权限的时候,原来应该是chmod -R 777 ./* 结果少按了个点,执行了chmod -R 777 /* 因为执行时间超出自己的预 ...