zoukankan      html  css  js  c++  java
  • 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.

    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);
            }
        }
    }
    

      

  • 相关阅读:
    社交需求和社交产品的更替
    腾讯产培生面经
    【C++基础】类class
    【C++基础】结构struct
    【C++基础】C-串知识整理
    GeoServer war包在tomcat7中配置遇到的一个问题
    pgrouting 2.0 的环境配置
    阿里2014年9月笔试中的一个算法设计题--擦黑板剩余数字
    VisualSVN Server的启动关闭脚本
    二叉树遍历(前序、中序、后序)的递归及非递归实现(小结)
  • 原文地址:https://www.cnblogs.com/apanda009/p/7903623.html
Copyright © 2011-2022 走看看