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));
}
}
}

geeksforgeeks@ Largest Number formed from an Array的更多相关文章

  1. [LeetCode179]Largest Number

    题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...

  2. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  3. 【leetcode】Largest Number

    题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...

  4. leetcode 179. Largest Number 求最大组合数 ---------- java

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  5. LeetCode-179. Largest Number

    179. Largest Number Given a list of non negative integers, arrange them such that they form the larg ...

  6. 【leetcode】Largest Number ★

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. 179. Largest Number -- 数字字符串比较大小

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

随机推荐

  1. NDK(8)"Unknown Application ABI"的解决方案

    ndk 调试本地应用时 报错如下 : console信息: [2015-08-17 19:52:05 - NdkSample] Unknown Application ABI: [2015-08-17 ...

  2. 如何获取supersocket的源代码

    源代码的地址:https://github.com/kerryjiang/SuperSocket 安装git之后,可以使用命令行git clone https://github.com/kerryji ...

  3. Android项目真的要去做混淆(加密)处理

    以前做项目做是懒得混淆代码,因为要处理各种第三方的混淆东西,像友盟里面加了第三方库,又要特殊处理混淆操作,所以很麻烦,也懒得去做混淆操作,so 你懂的:但今天我用一个反编译工具,发现一个很可怕的事情 ...

  4. 用VS2005开发WinCE程序调试图文教程

    一.WinCE 模拟器通过ActiveSync 6.1(即Windows Mobile设备中心)连接P 1.启动WinCE模拟器 命令行:  start .\DeviceEmulator.exe WI ...

  5. 读取Properties文件工具类

    import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...

  6. 安卓WebView中接口隐患与手机挂马利用(远程命令执行)

    安卓应用存在安全漏洞,浏览网站打开链接即可中招.目前有白帽子提交漏洞表明目前安卓平台上的应用普遍存在一个安全漏洞,用户打开一个链接就可导致远程安装恶意应用甚至完全控制用户手机,目前微信,手机QQ,QV ...

  7. libserialport: cross-platform library for accessing serial ports

    /*********************************************************************************** * libserialport ...

  8. python练习程序(c100经典例13)

    题目: 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数. for i in range(100,1000): a=i/100; b=(i/10)%10; c=i%1 ...

  9. python练习程序(c100经典例3)

    题目: 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? for i in range(1,100000): a=i+100; b=a+168; sa=int ...

  10. MySQL内存表-临时表

    HEAP表是访问数据速度最快的MySQL表,他使用保存在内存中的散列索引.但如果MySQL或者服务器重新启动,表中数据将会丢失.用法:如论坛的在线人数统计,这种表的数据应该是无关紧要的,就几个简单的字 ...