日期:2014-05-20  浏览次数:20825 次

组成最小的数
输入一个数字:
比如1432  然后输入一个数,比如为2,
那就等于要从1432中去除两个数字,使得去掉后的数是最小的~
比如去掉后就是:12  是最小的~求应该怎样做
------最佳解决方案--------------------
 count <= 0) {
           throw new RuntimeException("The count is out range [" + 1 + ", " + bits.length + "]");
       }

        List<Integer> allPosibility = new ArrayList<Integer>();
       integers(bits, 0, 0, count, 0, allPosibility);

       Collections.sort(allPosibility);
       return allPosibility.get(0);
   }

    public static void integers(int[] bits, int startIndex,
                                int currentCount, int maxCount,
                                int posibility,
                                List<Integer> allPosibility) {
        if (currentCount >= maxCount) {
            allPosibility.add(posibility);
            return;
        }

        for (int i = startIndex; i < bits.length; ++i) {
            posibility = posibility * 10 + bits[i];
            integers(bits, i + 1, currentCount + 1, maxCount, posibility, allPosibility);
            posibility /= 10;
        }
    }

    public static int[] bitsOfInteger(int n) {
        List<Integer> temp = new ArrayList<Integer>();

        do {
            temp.add(n % 10);
            n /= 10;
        } while (n != 0);

        int length = temp.size();
        int[] result = new int[length];
        for (int i = 0; i < length; ++i) {
            result[i] = temp.get(length - 1 - i);
        }

        return result;