【算法实验 Lab-report-unit-3-01】

文章目录


问题描述

(1) Largest Number. Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string instead of an integer.


思路

首先需要对原来的数组进行特殊排序,对于小于10的数字直接进行比较,而大于10 的数字需要进行处理,首先比较首位数字大小,如30和444,取得首位3和4,判断4>3,还有一种情况是例如30和3,若第一位数字相同,则比较第二位的数字0和3,很明显0要小于3,因此这里3>30。然后按照这个排序方法对原数组进行排序的同时,引入StringBuilder变量进行拼接。


代码

 public static StringBuilder getMaxInteger(int [] nums){
        StringBuilder builder = new StringBuilder();
        cut( nums, builder);
        return builder;
    }
    public static int [] cut(int [] nums,StringBuilder stringBuilder){
        if(nums.length < 2){
            return nums;
        }
        int half = nums.length >> 1;
        int[] left = Arrays.copyOfRange(nums, 0, half);
        int[] right = Arrays.copyOfRange(nums, half, nums.length);
        return merge(cut(left,stringBuilder),cut(right,stringBuilder),stringBuilder);
    }

    public static int [] merge(int [] left,int [] right,StringBuilder stringBuilder){
        int l = 0;
        int r = 0;
        int lTemp = left[l];
        int  rTemp = right[r];
        stringBuilder.delete(0, stringBuilder.length());
        int [] result = new int[left.length+right.length];
        for (int i = 0; i < result.length; i++) {

            if(l >= left.length){
                result[i] = right[r++];
                stringBuilder.append(result[i]);
            }else if( r >= right.length){
                result[i] = left[l++];
                stringBuilder.append(result[i]);
            }else if(isOverTen(left[l],lTemp,right[r],rTemp)){
                result[i] = left[l++];
                stringBuilder.append(result[i]);
            }else{
                result[i] = right[r++];
                stringBuilder.append(result[i]);
            }

        }
        return result;
    }
    public static boolean isOverTen(int num1,int temp1,int num2,int temp2){
        if(temp1 < 10){
            if(temp2 < 10){
                if(temp1 > temp2){
                    return true;
                }else if(temp1 == temp2 && num2 >= 10){
                    temp2 = num2 % 10;
                    return isOverTen( num1, temp1, num2, temp2);
                }else {
                    return false;
                }
            }else{
                temp2 /= 10;
                return isOverTen( num1, temp1, num2, temp2);
            }
        }
        temp1 /= 10;
        if(temp1 == temp2 && num1 >= 10){
            temp1 = num1 % 10;
            return isOverTen( num1, temp1, num2, temp2);
        }
         return isOverTen( num1, temp1, num2, temp2);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值