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         }
  • 相关阅读:
    人民币贬值之后该买什么
    elasticsearch系列(一): elasticsearch安装(docker方式)
    sql处理字符串: replace替换, substring_index拆分和处理json
    ab并发测试
    java反射--PropertyDescriptor类、Introspector类、BeanUtils库
    sql特殊用法: 查询添加, 查询更新, 添加或更新
    es查看和删除索引
    windows同步文件夹-增量同步最新修改
    mvn打包-指定模块
    grep过滤文本,awk,sort去重显示
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5063082.html
Copyright © 2011-2022 走看看