Problem

A number is called as a stepping number if the adjacent digits are having a difference of 1. For eg. 8,343,545 are stepping numbers. While 890, 098 are not. The difference between a ‘9’ and ‘0’ should not be considered as1. Given start number(s) and an end number (e) your function should list out all the stepping numbers in the range including both the numbers s & e.

Solution

注意一下边界 0 和 9 就好

 1 public static List<Integer> stepNumber(int s, int e) {
 2     List<Integer> res = new ArrayList<Integer>();
 3     if(s > e) return res;
 4
 5     int lens = (int) Math.floor(Math.log10(s) + 1);
 6     int lene = (int) Math.floor(Math.log10(e) + 1);
 7
 8     for(int i=lens; i<=lene; i++) {    //number in the length range of s & e
 9         for(int j=1; j<10; j++) {    //start head from 1 to 9
10             stepNumberHelper(res, s, e, i, j);
11         }
12     }
13
14     return res;
15 }
16
17 public static void stepNumberHelper(List<Integer> res, int s, int e, int length, int num) {
18     if(length-1 == 0) {
19         if(num >= s && num <= e) {
20             res.add(num);
21         }
22         return;
23     }
24
25     int lastDigit = num % 10;
26     if(lastDigit == 0) {
27         stepNumberHelper(res, s, e, length-1, num*10+1);
28     }
29     else if(lastDigit == 9) {
30         stepNumberHelper(res, s, e, length-1, num*10+8);
31     }
32     else {
33         stepNumberHelper(res, s, e, length-1, num*10+lastDigit+1);
34         stepNumberHelper(res, s, e, length-1, num*10+lastDigit-1);
35     }
36 }

转载于:https://www.cnblogs.com/superbo/p/4112121.html

Stepping Number相关推荐

  1. Intel处理器Family、Model、Stepping等的学习

    本文主要介绍在Linux系统中查看本机CPU信息方法,接着针对intel的x86处理器标识进行概括性描述,包括CPU ID.vendor.Family.Model.Stepping等.这些信息主要来自 ...

  2. 南京大学java机试,2019年南京大学计算机考研复试机试真题

    目录 第一题 Stepping Numbers 题意 思路 代码 反思 第二题 Nodes from the Root 题意 思路 代码 大佬的标准题解代码: 菜鸡我的又费空间,又费时间,又臭又长,思 ...

  3. input属性为number,maxlength不起作用的解决方案

    <input type="text" maxlength="11" /> 效果ok, 当 <input type="number&q ...

  4. ORA-19502: write error on file xxxxx, block number xxxx

    错误现象: 在ORACLE 10g下为表空间IGNITE_EGVSQL01增加数据文件时,报如下错误: SQL> ALTER TABLESPACE IGNITE_EGVSQL01      AD ...

  5. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  6. HDU 1711 Number Sequence(KMP算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/ ...

  7. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  8. [BuildRelease]build number / id

    build number, 也称为build id, 在build release的流程中唯一标示一个build,也是正式的产品的product version 和file version后两位(Ma ...

  9. Oracle-ORA-01722 invalid number错误

    本来正常的,经过抓包才知道原来是数字型的无意中多了乱码! 本来是3276的居然多了后面一串 3276PuAnrSeU2zliU+IV/FHlnX2Xgia1au2xX2vMWtw http://www ...

  10. input输入框为number类型时,去掉上下小箭头

    input输入框type为number时,去掉上下小箭头,方式如下: <input type="number" ...><style>/* 在Chrome浏 ...

最新文章

  1. 面向中小企业的视频云服务 视频托管
  2. “数学不好,干啥都不行!”骨灰级程序员:其实你们都是瞎努力!
  3. linux在内核下使用iic,实例解析linux内核I2C体系结构(2)
  4. 两种方法可以达到背景图不缩放的效果
  5. linux下的I2C驱动记录(RK)
  6. 逆向so_安卓逆向 | 分析调试与so调用实战
  7. noi.ac #543 商店
  8. securecrt调试c语言程序,SecureCRT脚本编写常用函数之WaitForString
  9. WebSecurityConfigurerAdapter详解
  10. 好程序员训练营-Java变量的作用域
  11. java文件迁移工具,Java的迁移
  12. 什么是信息系统安全等级保护
  13. 10019---SpringBoot简介
  14. 输入上/下标数字以及字母
  15. 国际金融学试题及参考答案
  16. dgiot百亿级物流标签轨迹时序数据压测
  17. 手机端linux虚拟机,教你如何用「UTM虚拟机」让苹果手机运行Windows/安卓/Linux系统...
  18. prometheus启动报错Unit prometheus.service entered failed state.
  19. android opengl es 圆锥纹理贴图
  20. Alpha对冲策略的原理是什么

热门文章

  1. QT 任务栏图标显示问题
  2. u盘在电脑上读不出来?从这几方面排查
  3. 《秘密》BY东野圭吾
  4. 【论文写作】——设置中英文字体
  5. 【寻找最佳小程序】12期:小程序数据助手——微信官方打造,移动端数据分析工具...
  6. Linux TCP之sack(二)
  7. 湘潭十八中2021年高考成绩查询,2021年 湖南省湘潭市高中学校排名top10
  8. jle汇编_x86汇编指令集大全(带注释)
  9. Todd.log - a place to keep my thoughts on programming
  10. 31 Three.js的特殊光源镜头光晕(lens flare)