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 Listres = new ArrayList (); 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 =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 findStrobogrammatic(int n) {30 List res = new ArrayList ();31 if (n <= 0) return res;32 build(res, "", n);33 return res;34 }35 36 public void build(List 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
曾经这样写,但是TLE
1 for (String str : res) {2 if ((str.length()>low.length() || str.compareTo(low)>=0) && (str.length()