1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set;
public class Scramble{ * Calculate the number of distinct scrambled pairs within the given range * @param min, the minimum value * @param max, the maximum value * @return the number of distinct scrambled pairs within the given range */ public static long scrambleCalculator(int min, int max){ if (min >= max){return 0;} Set<Integer> cache = new HashSet<Integer>(); long count = 0; for (int i = min; i <= max; i++){ if (!cache.contains(i)){ List<Integer> digits = intToDigits(i); Set<Integer> res = new HashSet<Integer>(); permutation(res, digits, min, max, 0, 0, digits.size()); count += countPairs((long) res.size()); if (res.size() > 1){ cache.addAll(res); } } } return count; } * Recursively visit the digits combination and get the permutations of the given * digits combination within the range. For example, if the digits are [1,2,3], * then we get the following permutations in order: 123, 132, 213, 231, 312, 321, * assuming the range is from 100 to 999. * @param total, a set to store the permutations that meet the requirements * @param digits, a list of digits of the original number * @param min, the minimum value * @param max, the maximum value * @param value, a permutation of the given digits when it reaches the length of the original integer * @param pos, the number of digits we have "built" for a permutation * @param length, the number of digits in the original integer */ public static void permutation(Set<Integer> total, List<Integer> digits, int min, int max, int value, int pos, int length){ if (pos == length){ if (min <= value && value <= max){ total.add(value); } return; } for (int i = 0; i < digits.size(); i++){ if (value != 0 || digits.get(i) != 0){ value = value * 10 + digits.get(i); digits.remove(i); permutation(total, digits, min, max, value, pos + 1, length); digits.add(i, value % 10); value /= 10; } } } * Compute the count of distinct pairs of elements in a set * @param size, total number of elements in a set * @return count of distinct pairs of elements */ public static long countPairs(Long size){ if (size < 2){return 0;} return (0 + size - 1) * size /2; } * Convert a positive integer to a list of digits in the integer. For * example, if the integer is 115, the method will return [5,1,1] * @param num, a positive integer. * @return a list of digits in the integer in reversed order */ public static List<Integer> intToDigits(Integer num){ List<Integer> ints = new ArrayList<Integer>(); if (num == null || num < 0){return ints;} while (num > 0){ int last = num % 10; ints.add(last); num /= 10; } return ints; } public static void main(String[] args){ int min = 10000000; int max = 25000000; long total = scrambleCalculator(min,max); System.out.println(total); } }
|