Ad
Code
Diff
  • import java.util.*;
    class Solution {
      
    	      private int[] getNumDigits(int number) {
            int[] digits = new int[10];
    
            while (number != 0) {
                digits[number % 10]++;
                number = number / 10;
            }
    
            return digits;
        }
    
        public int retSmallestPositiveInteger() {
    
            for (int num = 100; num < Integer.MAX_VALUE; num++) {
                boolean isEquals = true;
                int[] digits = getNumDigits(num);
                for (int i = 2; (i <= 6) && isEquals; i++) {
                    isEquals = isEquals && Arrays.equals(digits, getNumDigits(num * i));
                }
                if (isEquals)
                    return num;
            }
            return 0;
        }
    }
    • import java.util.*;
    • class Solution {
    • public static int retSmallestPositiveInteger() {
    • for(int i=1; ; i++) {
    • if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))
    • return i;
    • }
    • }
    • private static boolean hasSameDigits(int x, int y) {
    • char[] xdigits = Integer.toString(x).toCharArray();
    • char[] ydigits = Integer.toString(y).toCharArray();
    • Arrays.sort(xdigits);
    • Arrays.sort(ydigits);
    • return Arrays.equals(xdigits, ydigits);
    • }
    • private int[] getNumDigits(int number) {
    • int[] digits = new int[10];
    • while (number != 0) {
    • digits[number % 10]++;
    • number = number / 10;
    • }
    • return digits;
    • }
    • public int retSmallestPositiveInteger() {
    • for (int num = 100; num < Integer.MAX_VALUE; num++) {
    • boolean isEquals = true;
    • int[] digits = getNumDigits(num);
    • for (int i = 2; (i <= 6) && isEquals; i++) {
    • isEquals = isEquals && Arrays.equals(digits, getNumDigits(num * i));
    • }
    • if (isEquals)
    • return num;
    • }
    • return 0;
    • }
    • }