【leetcode】1215.Stepping Numbers
题目如下:
A 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 while421
is not.Given two integers
low
andhigh
, 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的更多相关文章
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- 【leetcode】 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- 【深入浅出-JVM】(1):Java 虚拟机
Java 虚拟机地位 种类 BEA的JRockit Solaris的Exact VM IBM的J9 感谢您的耐心阅读,如果您发现文章中有一些没表述清楚的,或者是不对的地方,请给我留言,您的鼓励是作者写 ...
- 切割nginx access日志
#!/bin/bash nginx_root=/www/server/nginx log_path=/www/wwwlogs yesterday=`date -d "-1 day" ...
- Python 3.8.0 正式版发布,新特性初体验 全面介绍
Python 3.8.0 正式版发布,新特性初体验 北京时间 10 月 15 日,Python 官方发布了 3.8.0 正式版,该版本较 3.7 版本再次带来了多个非常实用的新特性. 赋值表达式 PE ...
- websocket vue
/* eslint-disable */ let tt; let count = 1; let lockReconnect = false; function isJsonString(str) { ...
- C#面向对象22 委托事件反射
1.委托的定义:声明委托类型(返回值和参数,命名空间中):定义委托对象 (把委托想象成函数中的占位符~因为你并不确定调用哪个函数~) using System; using System.Collec ...
- EasyUI_前台js_省市县三级联动
1.html: <td class="tdl">所属城市</td> <td class="td_detail"> <i ...
- C#异步编程学习笔记之-async和await(续)
书接上文,本篇主要记录的内容要点:1.针对async和await在实际应用中的使用方式:2.异步方法返回值(有返回值和无返回值)的两种情况: 示例一(无返回值): using System; usin ...
- JS基础_打印出1-100之间所有的质数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础_运算符的优先级
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- (二)创建基于maven的javaFX项目
首先使用IDEA创建一个javaFX项目 点击finish,这就创建完成了JavaFX项目,只有将其转换为maven项目即可,如图: