原题链接在这里:https://leetcode.com/problems/stepping-numbers/

题目:

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的更多相关文章

  1. 【leetcode】1215.Stepping Numbers

    题目如下: A Stepping Number is an integer such that all of its adjacent digits have an absolute differen ...

  2. 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 ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  5. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  6. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. LeetCode Compare Version Numbers

    原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...

  9. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

随机推荐

  1. C++—lambda表达式+优先队列 prority_queue+关键字decltype

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入:[  1->4->5,  1->3->4,  2->6]输出: 1->1-&g ...

  2. c# EF插入数据报错跟踪代码

    我们在使用EF进行数据库插入的时候或出现一些插入失败的情况,但是具体是哪个字段不符合数据库设计要求无法得知,普通的try catch 无法捕获加上一下方法就可以 try            {    ...

  3. Spring Boot 注解大全,真是太全了!

    一.注解(annotations)列表  @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguratio ...

  4. Wampserver图标黄色解决

    本文章是参考了该网址https://jingyan.baidu.com/article/48b37f8d0a02811a6564887b.html 安装了Wampserver后,并对httped.co ...

  5. 关闭windows防火墙命令

    windows PowerShell (管理员) 或 CMD (管理员) 查看当前防火墙状态:netsh advfirewall show allprofiles 关闭防火墙:netsh advfir ...

  6. wind安装Jenkins+sonar+jdk

    最近公司在用Jenkins持续集成软件,自己研究的头痛,而且还是和C#项目融合到一起的,网上看到的都是Java的,我自己配了一套和C#的,和你们分享. Jenkins是一个开源软件项目,旨在提供一个开 ...

  7. 2019 华云数据java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.华云数据等公司offer,岗位是Java后端开发,最终选择去了华云数据. 面试了很多家公司,感觉大部分公司考察的点 ...

  8. Java自学-控制流程 switch

    Java的 switch 语句 switch 语句相当于 if else 的另一种表达方式 示例 1 : switch switch可以使用byte,short,int,char,String,enu ...

  9. 使用jmeter对dubbo接口进行性能测试教程及常见问题处理

    一.   测试脚本编写 脚本可参考git项目: https://github.com/aland-1415/dubbo-interface-test.git 1. pom依赖 (注意添加的jmeter ...

  10. 浅谈JS中 var let const 变量声明

    浅谈JS中 var let const 变量声明 用var来声明变量会出现的问题: 1. 允许重复的变量声明:导致数据被覆盖 2. 变量提升:怪异的数据访问.闭包问题 3. 全局变量挂载到全局对象:全 ...