Minimum Factorization

http://www.lintcode.com/zh-cn/problem/minimum-factorization/

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution {
    /**
     * @param a: a positive integer
     * @return: the smallest positive integer whose multiplication of each digit equals to a
     */
    public int smallestFactorization(int a) {
        // Write your code here
        List list = new ArrayList<>();
        while (a > 0) {
            if (a < 9) {
                list.add(a);
                break;
            } else {
                int size = list.size();
                for (int i = 9; i > 1; i--) {
                    if (a % i == 0) {
                        list.add(i);
                        a /= i;
                        break;
                    }
                }
                if (size == list.size()) {
                    return 0;
                }
            }
        }
        Collections.sort(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            Integer integer = list.get(i);
            sb.append(integer);
        }
        try {
            return Integer.parseInt(sb.toString());
        } catch (Exception e) {
            return 0;
        }
    }
}

你可能感兴趣的:(Minimum Factorization)