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 ...
随机推荐
- Django-07-Model操作
一.数据库的配置 1. 数据库支持 django默认支持sqlite.mysql.oracle.postgresql数据库 <1> sqlite django默认使用sqlite的数据库 ...
- Ubuntu 编译安装 qt-opensource 5.9
平台 :Ubuntu 18.04 QT版本 :5.9.1 (open source) g++ : 7.3.0arm-gcc :4.8.1 qt 需要 gcc4.8版本以上 下载解压,进入对应的 ...
- Docker容器挂载文件(转载)
一.Docker pull 安装 Nginx 1.查看docker仓库中的 nginx 命令 # 使用 docker search 命令搜索存放在 Docker Hub 中的镜像 docker sea ...
- python_封装redis_hash方法
xshell 进入 虚拟环境 安装 redis workon py3env # 进入虚拟环境 pip install redis # 安装redis deactivate # 退出虚拟环境 简单的封装 ...
- matplotlib 中文乱码问题
matplotlib是Python著名的绘图库,默认并不支持中文显示,因此在不经过修改的情况下,无法正确显示中文. 本文将介绍解决这一问题的方法. 不修改文件,加两行代码即可: matplotlib. ...
- ArcGIS加载数据中常用的File文件方法总结
在介绍ArcGIS中各种数据的打开方法时,我们用到了许多对于File文件的操作,在此做一个常用用法的总结.例如, 介绍ArcGIS中各种数据的打开方法——mxd(地图文档) 以方法一为例:运用Load ...
- Go defer 会有性能损耗,尽量不要用?
上个月在 @polaris @轩脉刃 的全栈技术群里看到一个小伙伴问 “说 defer 在栈退出时执行,会有性能损耗,尽量不要用,这个怎么解?”. 恰好前段时间写了一篇 <深入理解 Go def ...
- 矩量母函数(Moment Generating Function,mgf,又称:动差生成函数)
在统计学中,矩又被称为动差(Moment).矩量母函数(Moment Generating Function,简称mgf)又被称为动差生成函数. 称exp(tξ)的数学期望为随机变量ξ的矩量母函数,记 ...
- oracle plsql基本语法
oracle plsql 基本语法 --plsql默认规则:plsql赋值用":=" plsql判断用"=" plsql输入用"&" ...
- Spring Data Solr入门小Demo
package com.offcn.pojo; import java.io.Serializable; import java.math.BigDecimal; import java.util.D ...