题目如下:

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

解题思路:如果x是一个Stepping Number,假设x的个位是y,那么x*10 + y - 1 (y-1 >=0) 和 x*10 + y + 1 (y+1<=9) 也是Stepping Number,根据这个规律把所有符合条件的数字求出来即可。

代码如下:

class Solution(object):
def countSteppingNumbers(self, low, high):
"""
:type low: int
:type high: int
:rtype: List[int]
"""
queue = range(0,10)
res = set()
while len(queue) > 0:
val = queue.pop(0)
if val >= low and val <= high:
res.add(val)
last = int(str(val)[-1])
if last < 9:
new_val = int(str(val) + str(last+1))
if new_val <= high:
queue.append(new_val)
if last > 0:
new_val = int(str(val) + str(last-1))
if new_val <= high:
queue.append(new_val)
return sorted(list(res))

【leetcode】1215.Stepping Numbers的更多相关文章

  1. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  2. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【leetcode】Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...

  4. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.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(middle) ☆

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

  8. 【Leetcode】357. Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

  9. 【leetcode】 Add Two Numbers

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

随机推荐

  1. SQLSERVER2008 内存占用高的处理方式

    原文:SQLSERVER2008 内存占用高的处理方式 方法一: 方法二: 使用以下语句查找出什么语句占内存最高,针对占内存高的语句进行优化SELECT SS.SUM_EXECUTION_COUNT, ...

  2. iterm2 vim 开启滚轮

    之前使用mac自带终端时,可以通过上下滑动触摸板来在vim中快速浏览上下文.最近听说iterm2功能更加强大,索性试一试.发现默认没有这个功能,感觉应该可以通过配置实现,于是在iterm2的prefe ...

  3. MHA原理及搭建

    MYSQL5.7下搭建MHA 环境说明 在主机1,主机2,主机3上安装MySQL服务端和客户端. 主机1 主机2 主机3 操作系统 CentOS7.4 CentOS7.4 CentOS7.4 主机名 ...

  4. T100——自动执行设置了但没执行

    azzi950设置了背景定时执行,但到底没执行, 重启的命令如下.三个命令都执行一下.指令 1: r.r azzp950 kill 指令 2: r.r azzp951 kill 指令 3: r.r a ...

  5. isEmpty 和 isBlank 区别

    isEmpty 和 isBlank 区别 org.apache.commons.lang.StringUtils 类提供了 String 的常用操作,最为常用的判空有如下两种 isEmpty(Stri ...

  6. C#中判断文件夹或文件是否存在的方法

    一.根据虚拟路径获取文件物理路径: string savePath = Server.MapPath("~/Uploads/RemoteDatum/"); 二.判断文件夹是否存在 ...

  7. nodejs和npm

    Node.js安装及环境配置之Windows篇:https://www.cnblogs.com/liuqiyun/p/8133904.html 淘宝NPM镜像:https://npm.taobao.o ...

  8. pycharm问题

    Pycharm 出现Unresolved reference '' 错误的解决方法:http://www.mamicode.com/info-detail-2190842.html

  9. opencv ORB各参数的含义

    ORB中有很多参数可以设置,在OpenCV中它可以通过ORB来创建一个ORB检测器. ORB::ORB(int nfeatures=500, float scaleFactor=1.2f, int n ...

  10. SpringMVC——正常访问静态文件,不要找不到静态文件报404的方法

    方案一:激活Tomcat的defaultServlet来处理静态文件 <span style="font-size:12px;"> <servlet-mappin ...