原题链接在这里: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. redis学习(一)

    Redis学习内容: 1. 概念 2. 下载安装R 3. 命令操作 1. 数据结构 4. 持久化操作 5. 使用Java客户端操作redis 1. 概念: redis是一款高性能的NOSQL系列的非关 ...

  2. HTML文件直接在浏览器打开和本地服务器localhost打开有什么区别?

    最直接的区别,很容易注意到,一个是file协议,另一个是http协议. file协议更多的是将该请求视为一个本地资源访问请求,和你使用资源管理器打开是一样的,是纯粹的请求本地文件. http请求方式则 ...

  3. Jenkins+Gitlab配置Webhook实现提交自动部署

    一.概述 在上一篇文章,链接如下: https://www.cnblogs.com/xiao987334176/p/11434849.html 已经实现了 Jenkins+harbor+gitlab+ ...

  4. Kafka 系列(一)—— Kafka 简介

    一.简介 ApacheKafka 是一个分布式的流处理平台.它具有以下特点: 支持消息的发布和订阅,类似于 RabbtMQ.ActiveMQ 等消息队列: 支持数据实时处理: 能保证消息的可靠性投递: ...

  5. 论文笔记:DeepCF

    Abstract 推荐系统可以看作用户和物品的匹配问题,不过user以及item两者的语义空间差异太大,直接匹配不太符合实际.主流的改进CF的方法有两类:基于表示学习的CF方法以及基于函数学习的表示方 ...

  6. vue设置全局变量和修改

    1. 只读的全局变量 对于只读的全局变量,知道的有以下两种使用方式: 1)global.js 模块中定义:其他模块import后再使用即可 1.1)定义 import Vue from 'vue'; ...

  7. 切换GCC编译器版本

    当前版本信息 root@ubuntu:runninglinuxkernel_4.0# aarch64-linux-gnu-gcc -v Using built-in specs. COLLECT_GC ...

  8. linux 流量监控利器:iftop

    在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息. 查看网络状态可以使用netstat.nmap等工具. 若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. if ...

  9. linux搭建GitLab

    GitLab CentOS6 1. 安装VMware和CentOS 2. 安装必备Linux插件 3. 准备安装GitLab 4. 开始安装GitLab 5. 配置GitLab 6. 启动GitLab ...

  10. Spring Security 解析(二) —— 认证过程

    Spring Security 解析(二) -- 认证过程   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...