• 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;
            }
        }
  • 相关阅读:
    sort()函数与qsort()函数
    个人作业——软件工程实践总结作业
    个人作业——软件评测
    软件工程实践2019第五次作业
    软件工程实践2019第四次作业
    软件工程实践2019第三次作业
    软件工程实践2019第二次作业
    期末大作业
    第7次实践作业
    第6次实践作业
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5599454.html
Copyright © 2020-2023  润新知