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 ...
随机推荐
- for循环与if条件语句的复习运用
鉴于前面学了不少基础了,今天没有学新的内容.boyfriend给我出了几道简单的题目,慢慢的进步中. 1.# 计算1-100之间所有偶数的和 def sum(): sumone = 0 for i i ...
- PTA A1016
A1016 Phone Bills (25 分) 题目内容 A long-distance telephone company charges its customers by the followi ...
- 《JAVA高并发编程详解》-volatile和synchronized
- .NET Core入门
.Net core MVC 如何使用 .NET Core,最基本的入行,很多博客以及官网都有的太多太多的例子,但是大部分没有人做到了真的让一个小白一步一步的去学, 我第一次接触的时 ...
- python 2.django的镜像安装与第一次项目创建-运行以及app创建
django的设计模式 Django是一个遵循MVC设计模式的框架,MVC是Model.View.Controller的三个单词的简写.分别代表模型.视图.控制器. 而Django也是是一个MTV的设 ...
- Flutter — IDE Shortcuts for Faster Development
https://medium.com/flutter-community/flutter-ide-shortcuts-for-faster-development-2ef45c51085b If yo ...
- js预编译环节 变量声明提升 函数声明整体提升
预编译四部曲 1.创建AO对象 2.找形参和变量声明,将变量和形参名作为AO属性名,值为undefined 3.将实参和形参统一 4.在函数体里面找函数声明,值赋予函数体 function fn(a) ...
- Android为TV端助力记录EditText.setInputType的坑
如XML中设置android:inputType=”numberDecimal”在Java代码中仅设置setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) ...
- java-检查IP和端口在超时时间内是否可以连接
检查IP和端口在超时时间内是否可以连接 /** * 检查IP和端口是否可以连接 * @param ip * @param port * @return */ private static boolea ...
- Flask入门很轻松(三)—— 模板
Jinja2模板引擎 转载请在文章开头附上原文链接地址:https://www.cnblogs.com/Sunzz/p/10959471.html Flask内置的模板语言,它的设计思想来源于 Dja ...