原题链接在这里: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. Java开发笔记(一百四十八)通过JDBC查询数据记录

    前面介绍了通过JDBC如何管理数据库,当时提到Statement专门提供了executeQuery方法用于查询操作,为什么查询操作这么特殊呢?这是因为其它语句跑完一次就了事了,顶多像insert.up ...

  2. 一秒可生成500万ID的分布式自增ID算法—雪花算法 (Snowflake,Delphi 版)

    概述 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种 ...

  3. 选择类排序 (简单选择排序,堆排序)— c语言实现

    选择类排序包括: (1)  简单选择排序 (2)树形选择排序 (3)堆排序 简单选择排序: [算法思想]:在第 i 趟简单选择排序中,从第 i 个记录开始,通过 n - i 次关键字比较,从 n - ...

  4. Android Studio中设置与Eclipse中 Ctrl+1 功能类似的快捷键

    Eclipse:Ctrl + 1 Android Studio: Alt + Enter 1. 首先当然是打开设置窗口啦,然后在IDE Settings 下找到Keymap 2. 在Keymap的搜索 ...

  5. Intellij IDEA最全的热键表(default keymap)

    Editing Ctrl + Space Basic code completion (the name of any class, method or variable) Ctrl + Shift ...

  6. 使用密码远程QQ时窗口闪退

    系统时间不一致,在QQ上使用密码远程时会闪退,把系统时间调到大概一致就行了.

  7. Visual Studio 2019 使用.Net Core 3.0 二

    一.遇到难题 在微软官方逛了一圈,看到了这个. 马上点击,进去看看什么情况. 1.安装previewVisual studio 2019 2.设置SDK previews in Visual Stud ...

  8. Python进阶----计算机基础知识(操作系统多道技术),进程概念, 并发概念,并行概念,多进程实现

    Python进阶----计算机基础知识(操作系统多道技术),进程概念, 并发概念,并行概念,多进程实现 一丶进程基础知识 什么是程序: ​   程序就是一堆文件 什么是进程: ​   进程就是一个正在 ...

  9. 编程风格统一配置EditorConfig

    EditorConfig 以纯原生无需任何插件支持 EditorConfig 代码风格配置的编辑器: Visual Studio 2017 开始添加了对 EditorConfig 的原生支持.Visu ...

  10. MySQL-CentOS7上安装Mysql5.7

    #安装 wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm .noarch.rpm yum instal ...