• [LeetCode] 248. Strobogrammatic Number III 对称数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.

    246. Strobogrammatic Number和247. Strobogrammatic Number II的拓展,求一个区间的数里有多少个对称数。有了247一题作基础,这里我们可以先用迭代方法求出所有长度满足的数,再通过与low,high的compare比较,选出最终的结果并count。

    Java:

    public class Solution {
        public List<String> helper(int n, int max) {
            if (n == 0) return new ArrayList<String>(Arrays.asList(""));
            if (n == 1) return new ArrayList<String>(Arrays.asList("1", "8", "0"));
            
            List<String> list = helper(n - 2, max);
            List<String> result = new ArrayList<String>();
            for (int i = 0; i < list.size(); i++) {
                String s = list.get(i);
                if (n != max) result.add("0" + s + "0");
                result.add("1" + s + "1");
                result.add("8" + s + "8");
                result.add("6" + s + "9");
                result.add("9" + s + "6");
            }
            return result;
        }
        
        public int strobogrammaticInRange(String low, String high) {
            int count = 0;
            List<String> result = new ArrayList<String>();
            for (int i = low.length(); i <= high.length(); i++) {
                result.addAll(helper(i, i));
            }
            for (String str : result) {
                if (str.length() == low.length() && str.compareTo(low) < 0 || str.length() == high.length() && str.compareTo(high) > 0) continue;
                count++;
            }
            return count;
        }
    }
    

      

    Java:

    public class Solution {
        private int count = 0;
        private Map<Character, Character> map = new HashMap<>();
         
        public int strobogrammaticInRange(String low, String high) {
            if (low == null || low.length() == 0 || high == null || high.length() == 0) {
                return 0;
            }
             
            fillMap();
             
            for (int n = low.length(); n <= high.length(); n++) {
                char[] arr = new char[n];
                getStrobogrammaticNumbers(arr, 0, n - 1, low, high);
            }
             
            return count;
        }
         
        private void getStrobogrammaticNumbers(char[] arr, int start, int end, String low, String high) {
            if (start > end) {
                String s = new String(arr);
                if ((s.length() == 1 || s.charAt(0) != '0') && compare(low, s) && compare(s, high)) {
                    count++;
                }
                return;
            }
                 
            for (char c : map.keySet()) {
                arr[start] = c;
                arr[end] = map.get(c);
                     
                if ((start < end) || (start == end && map.get(c) == c)) {
                    getStrobogrammaticNumbers(arr, start + 1, end - 1, low, high);
                }
            }
        }
         
        // Return true if s1 <= s2
        private boolean compare(String s1, String s2) {
            if (s1.length() == s2.length()) {
                if (s1.compareTo(s2) <= 0) {
                    return true;
                } else {
                    return false;
                }
            }
             
            return true;
        }
         
        private void fillMap() {
            map.put('0', '0');
            map.put('1', '1');
            map.put('8', '8');
            map.put('6', '9');
            map.put('9', '6');
        }
    }
    

     

    类似题目:

    [LeetCode] 246. Strobogrammatic Number 对称数

    [LeetCode] 247. Strobogrammatic Number II 对称数II 

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    头条java 后台一面凉经
    JVM运行时数据区
    基础篇——Spring Cloud Hystrix
    bug篇——idea拉取代码认证失败重新登录
    基础篇——代理模式之SpringAOP
    基础篇——代码优化100条之(11—20)
    电商项目实战(架构八)——RabbitMQ实现延迟消息
    电商项目实战(架构七)——Mongodb实现文档操作
    基础篇——代码优化100条之(1—10)
    电商项目实战(架构六)——Elasticsearch实现商品搜索
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8491313.html
Copyright © 2020-2023  润新知