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 ...
随机推荐
- flask---快速使用
初识falsk django是大而全面的框架,flask是个轻量级的框架. flask快速开发网站 flask可以使用很少的代码就可以直接完成一个项目(6,7行代码),如下: from flask i ...
- ASP.NET WebApi 学习与实践系列(1)---如何创建 WebApi
写在前面 最近在做一个app的时候发现需要写后台服务.所以,在考虑是使用webapi还是使用webserver来写这个后台服务的时候.爱纠结的我,最后还是选择了使用webapi来写这个后台服务. 原因 ...
- C# vb .net实现焦距柔化特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的焦距柔化效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- MongoDB和Java(7):MongoDB用户管理
最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...
- python 安装 SQLAlchemy 报错
安装 SQLAlchemy 报错 安装命令 pip install -i https://pypi.doubanio.com/simple SQLAlchemy 报错截图 编码错误,这里我们需要改下源 ...
- django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法
django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法 django.db ...
- 关于 Safari 浏览器不支持 location [ window.location.href window.open()] 跳转问题的解决方案
最近在做项目时,碰到 safari 浏览器不支持location跳转问题,针对此问题,可以通过 js 模拟点击时间来解决,代码如下: <!DOCTYPE HTML> <html la ...
- Celery:Daemonization
参考文档:http://docs.celeryproject.org/en/latest/userguide/daemonizing.html#daemonizing
- C# 里面将字符作为代码计算,主要是运算符号的计算
DataTable dt = new DataTable(); string str="1+2*(5+3)+3-1"; dt.Compute(str, "false&qu ...
- npm 更换阿里淘宝源
执行命令 npm config set registry https://registry.npm.taobao.org/ 查看是否已经更换成功 npm config get registry