[email protected] Largest Number formed from an Array

http://www.practice.geeksforgeeks.org/problem-page.php?pid=380

Largest Number formed from an Array

Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.

The result is going to be very large, hence return the result in the form of a string.

Input:

The first line of input consists number of the test cases. The description of T test cases is as follows:

The first line of each test case contains the size of the array, and the second line has the elements of the array.

Output:

In each separate line print the largest number formed by arranging the elements of the array in the form of a string.

Constraints:

1 ≤ T ≤ 70
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 1000

Example:

Input:

2
5
3 30 34 5 9
4
54 546 548 60

Output:

9534330
6054854654

import java.util.*;
import java.lang.*;
import java.io.*;

class Entry {
    public String str;
    public Entry(String s) {
        super();
        this.str = s;
    }
}

class cmp implements Comparator<Entry> {

    public int compare(Entry e1, Entry e2) {
        String s1 = e1.str;
        String s2 = e2.str;

        StringBuffer combo1 = new StringBuffer();
        StringBuffer combo2 = new StringBuffer();

        combo1.append(s1); combo1.append(s2);
        combo2.append(s2); combo2.append(s1);

        return (combo2.toString()).compareTo(combo1.toString());
    }
}

class GFG {

    public static String func(int[] arr) {

        int n = arr.length;
        ArrayList<Entry> ls = new ArrayList<Entry> ();
        for(int i=0; i<n; ++i) {
            Entry entry = new Entry(Integer.toString(arr[i]));
            ls.add(entry);
        }

        Collections.sort(ls, new cmp());
        StringBuffer sb = new StringBuffer();
        for(Entry entry: ls) {
            sb.append(entry.str);
        }
        return sb.toString();
    }

    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int times = in.nextInt();

        for(int i=0; i<times; ++i) {
            int n = in.nextInt();
            int[] arr = new int[n];
            for(int j=0; j<n; ++j) {
                arr[j] = in.nextInt();
            }
            System.out.println(func(arr));
        }
    }
}

时间: 2024-11-03 14:15:39

[email protected] Largest Number formed from an Array的相关文章

@codechef - [email&#160;protected] Random Number Generator

目录 @[email protected] @[email protected] @part - [email protected] @part - [email protected] @part - [email protected] @accepted [email protected] @[email protected] @[email protected] 给定递推关系式:\[A_i=C_1A_{i-1} + C_2A_{i-2}+\dots+C_kA_{i-k}\] 并给定 \(A_

[email&#160;protected] [200] Number of Islands

https://leetcode.com/problems/number-of-islands/ Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume

[email&#160;protected] [33] Search in Rotated Sorted Array

https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array r

[email&#160;protected] [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums

[LeetCode179]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

Shell特殊变量:Shell $0, $#, $*, [email&#160;protected], $?, $$和命令行参数

变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid,看下面的代码: $echo $$ 运行结果 29949 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $# 传递给脚本或函数的参数个数. $* 传递给脚本或函数的所有参数. [email protected] 传递给脚本或函数的所有参数.被

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 an i

[leetcode sort]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 string instead of an i

[每日编程]求 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在