leetcode Strobogrammatic Number III
来源:互联网 发布:数据库的安全防护措施 编辑:程序博客网 时间:2024/06/10 05:23
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.
For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
Note:
Because the range might be a large number, the low and high numbers are represented as string.
思路其实挺简单的,递归字符串长度直至大于等于low,小于等于high,到了这里之后需要考虑几种边界值的情况:
1、长度等于low,但值小于low
2、长度等于high,值大于high
3、首尾为0的情况,需要过滤掉
private int num=0;public int strobogrammaticInRange(String low, String high) { getSum(low,high,""); getSum(low,high,"8"); getSum(low,high,"1"); getSum(low,high,"0"); return num;}public void getSum(String low,String high,String str){ if(str.length()>=low.length()&&str.length()<=high.length()){ if(str.length()==low.length()&&str.compareTo(low)<0) return; if(str.length()==high.length()&&str.compareTo(high)>0) return; if(str.length()==1||(str.length()>1&&str.charAt(0)!='0')) num+=1; } if(str.length()+2>high.length()) return; getSum(low,high,"1"+str+"1"); getSum(low,high,"8"+str+"8"); getSum(low,high,"0"+str+"0"); getSum(low,high,"6"+str+"9"); getSum(low,high,"9"+str+"6");}
0 0
- leetcode Strobogrammatic Number III
- LeetCode 248. Strobogrammatic Number III
- LeetCode 题解(240) : Strobogrammatic Number III
- [leetcode] 248. Strobogrammatic Number III 解题报告
- [Leetcode] 248. Strobogrammatic Number III 解题报告
- [LeetCode248]Strobogrammatic Number III
- Leetcode: Strobogrammatic Number
- *LeetCode-Strobogrammatic Number
- [leetcode 246] Strobogrammatic Number
- Leetcode-246.Strobogrammatic Number
- LeetCode 246. Strobogrammatic Number
- LeetCode 246. Strobogrammatic Number
- Leetcode 246. Strobogrammatic Number
- Strobogrammatic Number -- Leetcode 246
- LeetCode 题解(238) : Strobogrammatic Number
- Leetcode NO.246 Strobogrammatic Number
- LeetCode 247. Strobogrammatic Number II
- LeetCode 247. Strobogrammatic Number II
- 同步容器&并发下的容器
- Linux目录结构和常用命令
- 如何使用CSDN-markdown编辑器
- Python(Dict和Set类型)
- Sun JVM 年轻代和老年代垃圾回收都需要暂停JVM
- leetcode Strobogrammatic Number III
- ListView中item与控件抢夺焦点的那些事
- c#学习
- 十七、中介者模式Mediator(行为型)
- Leetcode 40. Combination Sum II (Medium) (cpp)
- IOS学习之核心动画-复制层
- LeetCode 69 Sqrt(x)
- 关于C51的IE(中断使能寄存器)的初级认识
- Java源码:ArrayList的实现原理