[每日编程]求 largest Number - 给出一组非负整数,求这些非负整数可以拼接出的最大数字


英文:Given a list of non negative integers, arrange them such that they form the largest number.


中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字


说明:例如,给出数组 [3, 30, 34, 5, 9],拼接出的最大数字为9534330

正确的排序方法,是使用排序方法进行比较时,比较两个字符串(设为A和B),以先后顺序拼接而成的两个字符串A+B和B+A,如果A+B更大,则A在前B在后,否则A在后B在前。

public class Solution {

    public static String largestNumber(int[] nums)
    {
        String[] array = new String[nums.length];

        for(int i = 0; i < nums.length; i++)
        {
            array[i] = String.valueOf(nums[i]);
        }

        String temp;
        for(int i = 0; i < array.length; i++)
        {
            for(int j = i+1; j < array.length; j++)
            {
                if((array[i] + array[j]).compareTo(array[j] + array[i]) < 0)
                {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }

        if(array[0].equals("0"))
            return "0";
        else
        {
            return String.join("", array);
        }
    }

    public static void main(String[] args)
    {
        int[] nums = {3, 30, 34, 5, 9};

        String largestNum = largestNumber(nums);

        System.out.println(largestNum);
    }

}
时间: 2024-08-05 11:11:57

[每日编程]求 largest Number - 给出一组非负整数,求这些非负整数可以拼接出的最大数字的相关文章

leetcode上面的一个题目,求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 o

【c语言】输入一组整数,求出最大子序列的和

// 输入一组整数,求出最大子序列的和. // 例如:序列: - 2 11 - 4 13 - 5 - 2,则最大子序列和为20. // 序列: - 6 2 4 - 7 5 3 2 - 1 6 - 9 10 - 2,则最大子序列和为16 #include <stdio.h> int Max_Son(int *p, int len) { int Max_Sum = 0; int i, j; for (i = 0; i < len; ++i) { int sum = 0; for (j = i

LeetCode:Largest Number

1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetcode.com/problems/largest-number/ 3.题目内容 英文:Given a list of non negative integers, arrange them such that they form the largest number. 中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字 说明:例如,给出数组 [3, 30, 34,

No.179 Largest Number

No.179 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

[LeetCode] 179. Largest Number Java

题目: 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

[转]LeetCode: 128 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

[C++]LeetCode: 128 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

【LeetCode】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

使用Github Page鼓励自己每日编程

动机 三天不练手生,编程的基础训练本身是很枯燥的,需要很多的认真与坚持.无论是Debug的经验,语法规则的记忆,还是各类基础的算法运用,都需要持之以恒的认真.Github的"打卡"以及展示页,可以作为一个小小的奖励,鼓励自己每日坚持. 方案 因为github是支持jekyll引擎的,所以使用jekyll生成项目是足够好的选择,还有新一点的工具Hexo,都能快速地在Github Pages上面搭建blog.难度上会比WordPress大一点点,但是足够geek啊.花了一个下午折腾,终于搭