zoukankan      html  css  js  c++  java
  • 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         }
  • 相关阅读:
    面试官:重写 equals 时为什么一定要重写 hashCode?
    MyBatis 中为什么不建议使用 where 1=1?
    面试官:方法重写时需要注意哪些问题?
    Java中List排序的3种方法
    面试官:this和super有什么区别?this能调用到父类吗?
    面试官:int和Integer有什么区别?为什么要有包装类?
    HashMap 中的一个“坑”!
    Java 中接口和抽象类的 7 大区别!
    List 去重的 6 种方法,这个方法最完美!
    面试官:如何实现 List 集合去重?
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5063082.html
Copyright © 2011-2022 走看看