题目如下:

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. 基于Docker安装 GitLab

    ⒈下载镜像 本文使用GitLab 中文社区版 Docker 镜像 Docker Hub地址:https://hub.docker.com/r/beginor/gitlab-ce 如果要体验最新版的Gi ...

  2. # codeblocks 使用技巧+伪单文件编译

    codeblocks 使用技巧+伪单文件编译 shift+F2打开和隐藏左侧工作空间 F2 打开和隐藏下面控制台 CTRL+Shift+c 注释,CTRL+Shift+x取消注释 view->p ...

  3. idea模块在maven projects中显示灰色的解决办法

    如题,出现的情况如下图所示 解决方式 将√去掉,点击确定关闭,对maven进行刷新操作 解决

  4. PHP给图片添加文字水印实例

    PHP给图片添加文字水印实例,支持中文文字水印,是否覆盖原图,自定义设置水印背景色.文字颜色.字体等. 水印类water.class.php var $Path = "./"; / ...

  5. 简单使用template-web.js

    手册地址: http://aui.github.io/art-template/docs/syntax.html https://github.com/aui/art-template 原文地址: h ...

  6. 使用Qt 3D Studio 2.4显着提升性能(渲染速度提高了565%)

    发布于2019年6月18日星期二11评论Qt 3D Studio 2.4显着改善性能 发表于Biz Circuit&Dev Loop,设计,图形,性能,Qt 3D Studio 除了有效使用系 ...

  7. maraidb忘记数据密码

    一.概述 服务器上安装了maraidb 数据库,但是很久未使用过它,需要使用时,忘记了密码, 此时可以给它重新设置密码. 二.操作 修改密码 修改 /etc/my.cnf,修改下图红色区域位置,修改成 ...

  8. [C#.net]xlApp.Workbooks.Open打开无法远程访问

    上月还能使用的xlApp.Workbooks.Open,这个月报无法远程访问,搞了半天,才找到原因是Foxit PDF 的Execl插件搞的鬼,记录下 Excel.Workbooks wbChecks ...

  9. [转载]VS2005的工程用VS2010打开后,用VS2005不能打开的解决方法

    首先,在“项目”菜单里,把项目属性“目标平台”改为框架2.0,保存退出. 然后,用记事本或用编辑文本文件的方式打开你的项目文件,后缀为.sln 第一行:把“Microsoft Visual Studi ...

  10. Ubuntu14.04更新硬件实现堆栈(HWE)

    Ubuntu14.04更新硬件实现堆栈(HWE) 来源: https://github.com/gatieme/AderXCoding/tree/master/system/tools/ubuntu_ ...