Leetcode: Strobogrammatic Number III

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.

 1 public class Solution {
 2     public int strobogrammaticInRange(String low, String high) {
 3         int lowlen = low.length();
 4         int highlen = high.length();
 5         List<String> res = new ArrayList<String>();
 6         for (int i=lowlen; i<=highlen; i++) {
 7             res.addAll(findStrobogrammatic(i));
 8         }
 9         int i=0;
10         int count=res.size();
11         while(i<res.size() && res.get(i).length()==low.length()){
12             if(res.get(i).compareTo(low)<0){
13                 count--;
14             }
15             i++;
16         }
17         i=res.size()-1;
18         while(i>=0 && res.get(i).length()==high.length()){
19             if(res.get(i).compareTo(high)>0){
20                 count--;
21             }
22             i--;
23         }
24         return count;
25     }
26     
27     char[] dict = new char[]{'0', '1', '6', '8', '9'};
28     
29     public List<String> findStrobogrammatic(int n) {
30         List<String> res = new ArrayList<String>();
31         if (n <= 0) return res;
32         build(res, "", n);
33         return res;
34     }
35     
36     public void build(List<String> res, String item, int n) {
37         if (item.length() == n) {
38             res.add(item);
39             return;
40         }
41         boolean last = (item.length()==n-1);
42         for (int i=0; i<dict.length; i++) {
43             char c = dict[i];
44             if ((n!=1 && item.length()==0 && c=='0') || (last && (c=='6' || c=='9'))) 
45                 continue;
46             StringBuffer buf = new StringBuffer(item);
47             append(buf, last, c);
48             build(res, buf.toString(), n);
49         }
50     }
51     
52     public void append(StringBuffer buf, boolean last, char c) {
53         if (c == '6') buf.insert(buf.length()/2, "69");
54         else if (c == '9') buf.insert(buf.length()/2, "96");
55         else {
56             buf.insert(buf.length()/2, last? c : ""+c+c);
57         }
58     }
59 }

曾经这样写,但是TLE

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值