1. 程式人生 > >248. Strobogrammatic Number III

248. Strobogrammatic Number III

 (3)找出1-650中所有的ambiguous number,定義:如果把一個數upside down還是一個有效數的話,這個數就是ambiguous,比如19 -> 61, follow up找到1-n中的這種數



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. Example: Input: low = "50", high = "100" Output: 3 Explanation: 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. // solution 1 , use dfs . // 明天自己寫 https://leetcode.com/problems/strobogrammatic-number-iii/discuss/67378/Concise-Java-Solution
Construct char arrays from low.length() to high.length() Add stro pairs from outside When left > right, add eligible count private static final char[][] pairs = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}}; public int strobogrammaticInRange(String low, String high) { int
[] count = {0}; for (int len = low.length(); len <= high.length(); len++) { char[] c = new char[len]; dfs(low, high, c, 0, len - 1, count); } return count[0]; } public void dfs(String low, String high , char[] c, int left, int right, int[] count) { if (left > right) { // meet the middle String s = new String(c); // check if out of the the low and high if ((s.length() == low.length() && s.compareTo(low) < 0) || (s.length() == high.length() && s.compareTo(high) > 0)) { return; } count[0]++; return; } for (char[] p : pairs) { c[left] = p[0]; c[right] = p[1]; if (c.length != 1 && c[0] == '0') { // 0 can not be on the outside continue; } if (left == right && p[0] != p[1]) { // the middle element has to be the same as itself, like 0, 8, 1 continue; } dfs(low, high, c, left + 1, right - 1, count); } } // use strobe 2 // Memory Limit Exceeded https://leetcode.com/problems/strobogrammatic-number-iii/discuss/67406/Clear-Java-AC-solution-using-Strobogrammatic-Number-II-method class Solution{ public int strobogrammaticInRange(String low, String high) { // can use stro 2 to get all the stro numbers of certain length and check each number , if exists in the range int res = 0; int start = low.length(); int end = high.length(); List<String> list = new ArrayList<>(); for(int i = start; i <= end; start++){ list.addAll(findStrobogrammatic(i)); } for(String num : list){ if((num.length() == low.length()&&num.compareTo(low) < 0 ) ||(num.length() == high.length() && num.compareTo(high) > 0)) continue; res++; } return res; } private List<String> findStrobogrammatic(int n) { return helper(n, n); } private List<String> helper(int n, int m){ // base case // if(n == 0){ // return new ArrayList<>(""); // } // if(n == 1){ // return new ArrayList<>("1","8", "0"); // } if (n == 0){ return new ArrayList<String>(Arrays.asList("")); // Arrays.asList returns a fixed sized list } if (n == 1){ return new ArrayList<String>(Arrays.asList("1","0","8")); } List<String> prevResult = helper(n - 2, m); List<String> result = new ArrayList<>(); // two cases: if we are filling numbers on the very outside, we can add 1 6 8 9 // another case is if we are not on the very outside, we can add 1 6 8 9 0 for(String s : prevResult){ if(n != m){ // we are not on the very outside, which means we can fill up with 0 result.add("0" + s + "0"); } result.add("1" + s + "1"); result.add("6" + s + "9"); result.add("9" + s + "6"); result.add("8" + s + "8"); } return result; } }