zoukankan      html  css  js  c++  java
  • 248.Strobogrammatic Number III

        /*
         * 248.Strobogrammatic Number III
         * 2016-6-18 by Mingyang
         * Write a function to count the total strobogrammatic numbers 
         * that exist in the range of low <= num <= high.
         * Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
         * 大量的沿用了II的代码,非常不好!
         */
        public int strobogrammaticInRange(String low, String high) {
            if(low == null || high == null)
                return 0;
            int lo = low.length(), hi = high.length();
            int res = 0;     
            for(int i = lo; i <= hi; i++)
                res += findStrobogrammatic(i, low, high).size();       
            return res;
        }   
        private List<String> findStrobogrammatic(int n, String low, String high) {
            if(n < 1)
                return new ArrayList<String>();
            List<String> res = new ArrayList<String>();
            Map<Character, Character> map = new HashMap<Character, Character>();
            map.put('0', '0');
            map.put('1', '1');
            map.put('6', '9');
            map.put('8', '8');
            map.put('9', '6');     
            StringBuilder sb = new StringBuilder();
            int position = (n % 2 == 0) ? 0 : 1;
            findStrobogrammatic(res, sb, map, n, position, low, high);     
            return res;
        }   
        private void findStrobogrammatic(List<String> res, StringBuilder sb, Map<Character, Character> map, int n, int position, String low, String high) {
            if(sb.length() > n)
                return;
            if(sb.length() == n) {
                String s = sb.toString();
                if(firstStringEqualToOrSmaller(low, s) && firstStringEqualToOrSmaller(s, high))
                    res.add(sb.toString());
                return;
            }     
            if(position == 1) {
                for(char c : map.keySet()) {
                    if(c == '6' || c == '9')
                        continue;
                    sb.append(c);
                    findStrobogrammatic(res, sb, map, n, position + 1, low, high);
                    sb.setLength(0);
                }
            } else {
                for(char c : map.keySet()) {
                    if(n - sb.length() == 2 && c == '0')
                        continue;
                    sb.insert(0, c);
                    sb.append(map.get(c));
                    findStrobogrammatic(res, sb, map, n, position + 2, low, high);
                    sb.deleteCharAt(0);
                    sb.deleteCharAt(sb.length() - 1);
                }    
            }
        }  
        private boolean firstStringEqualToOrSmaller(String s, String t) {
            if(s.length() < t.length())
                return true;
            else if(s.length() > t.length())
                return false;
            else {
                for(int i = 0; i < s.length(); i++)
                    if(s.charAt(i) > t.charAt(i))
                        return false;
                    else if(s.charAt(i) < t.charAt(i))
                        return true;
                return true;
            }
        }
  • 相关阅读:
    中断子系统5_电流层处理
    太极拳透劲的原理推测
    OpenSSL命令---pkcs7
    初步分析美国科研项目申报项目
    OpenSSL命令---pkcs8
    C#中Hashtable的用法
    android 中 ViewPager 的平常用法 ViewPager+ Views
    打卡汇报
    OpenSSL命令---pkcs12
    卡卡游戏引擎之快速入门
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5599454.html
Copyright © 2011-2022 走看看