My Java code, deal with overflow


  • 0
    public int smallestFactorization(int a) {
            if(a < 10) return a;
            int[] res = new int[10];
            int i = 0, j = 0;
            for(i = 9; i > 1; i--) {
                while(a % i == 0) {
                    if(j >= 10) return 0;
                    a /= i;
                    res[j] = i;
                    j++;
                }
            }
            if(a > 10) return 0;
            StringBuilder sb = new StringBuilder();
            for(i = j - 1; i >= 0; i--) {
                sb.append(res[i]);
            }
            String r = sb.toString();
            try {
                return Integer.parseInt(r);
            } catch(NumberFormatException n) {
                return 0;
            }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.