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.

Strobogrammatic Number II give us all the strings with length n, then we can call it to get strings with length between low.length() and high.length(), and remove the strings that less than low and larger than high.

 public class Solution {
public int strobogrammaticInRange(String low, String high) {
int lowlen = low.length();
int highlen = high.length();
List<String> res = new ArrayList<String>();
for (int i=lowlen; i<=highlen; i++) {
res.addAll(findStrobogrammatic(i));
}
int i=0;
int count=res.size();
while(i<res.size() && res.get(i).length()==low.length()){
if(res.get(i).compareTo(low)<0){
count--;
}
i++;
}
i=res.size()-1;
while(i>=0 && res.get(i).length()==high.length()){
if(res.get(i).compareTo(high)>0){
count--;
}
i--;
}
return count;
} char[] dict = new char[]{'0', '1', '6', '8', '9'}; public List<String> findStrobogrammatic(int n) {
List<String> res = new ArrayList<String>();
if (n <= 0) return res;
build(res, "", n);
return res;
} public void build(List<String> res, String item, int n) {
if (item.length() == n) {
res.add(item);
return;
}
boolean last = (item.length()==n-1);
for (int i=0; i<dict.length; i++) {
char c = dict[i];
if ((n!=1 && item.length()==0 && c=='0') || (last && (c=='6' || c=='9')))
continue;
StringBuffer buf = new StringBuffer(item);
append(buf, last, c);
build(res, buf.toString(), n);
}
} public void append(StringBuffer buf, boolean last, char c) {
if (c == '6') buf.insert(buf.length()/2, "69");
else if (c == '9') buf.insert(buf.length()/2, "96");
else {
buf.insert(buf.length()/2, last? c : ""+c+c);
}
}
}

曾经这样写,但是TLE

         for (String str : res) {
if ((str.length()>low.length() || str.compareTo(low)>=0) && (str.length()<high.length() || str.compareTo(high)<=0))
count++;
}

Leetcode: Strobogrammatic Number III的更多相关文章

  1. [LeetCode] Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. [LeetCode] Strobogrammatic Number II 对称数之二

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  3. [LeetCode] Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  4. LeetCode Strobogrammatic Number II

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...

  5. LeetCode Strobogrammatic Number

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...

  6. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  7. [LeetCode] 248. Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  8. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  9. 248. Strobogrammatic Number III

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

随机推荐

  1. 11.PHP内核探索:嵌入式PHP PHP内核探索:嵌入式PHP

    从PHP源码目录结构的介绍以及PHP生命周期可知:嵌入式PHP类似CLI,也是SAPI接口的另一种实现. 一般情况下,它的一个请求的生命周期也会和其它的SAPI一样:模块初始化=>请求初始化=& ...

  2. P2882 Face The Right Way - USACO07MAR

    这道题没有一个比较详细的题解,我来提供一份. 首先我们可以知道,反转区间的顺序对结果没有影响,而且一个区间如果翻转两次以上是没有意义的,所以,问题就变成了求哪些区间需要反转. 我们枚举k.对于每一个k ...

  3. php配置相关

    php.ini error_reporting //配置错误的显示方式 display_errors //此项优先级高于error_reporting,如果关闭该项,会返还http状态码,如果开启,则 ...

  4. UltraEdit 标签(tab)不见的3个解决办法

    UltraEdit 标签(tab)不见的3个解决办法 2010-11-08 09:19 1042人阅读 评论(0) 收藏 举报 工具c 方法1:点 视图->视图/列表(V)->打开文件标签 ...

  5. (转)freemakeer初入门

    在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...

  6. BLE 4.0 与 4.1的区别

    蓝牙技术让我们在连接各种设备的时候不再被繁多的数据线所束缚,比如音响.电脑,甚至是汽车.目前最新的蓝牙版本是4.0,相比3.0它进一步降低了功耗,并且也提高了传输效率.近日,蓝牙技术联盟(Blueto ...

  7. HTML5音频,视频播放

    1.此方法可支持多种浏览器 <audio controls="controls"> <source src="1.mp3" ></ ...

  8. 是什么在.NET程序关闭时阻碍进程的退出?

    在平时使用软件或是.NET程序开发的过程中,我们有时会遇到程序关闭后但进程却没有退出的情况,这往往预示着代码中有问题存在,不能正确的在程序退出时停止代码执行和销毁资源.这个现象有时并不容易被察觉,但在 ...

  9. [LeetCode] Regular Expression Matching(递归)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  10. Combine String---hdu5727 &&& Zipper(LCS变形)

    题目链接:http://poj.org/problem?id=2192 http://acm.split.hdu.edu.cn/showproblem.php?pid=5707 http://acm. ...