Construct minimum number by reordering a given non-negative integer array. Arrange them such that they form the minimum number.

Notice

The result may be very large, so you need to return a string instead of an integer.

Have you met this question in a real interview?

 
 
Example

Given [3, 32, 321], there are 6 possible numbers can be constructed by reordering the array:

3+32+321=332321
3+321+32=332132
32+3+321=323321
32+321+3=323213
321+3+32=321332
321+32+3=321323

So after reordering, the minimum number is 321323, and return it.

分析:

这里需要对数组进行排序,那么怎么比较大小呢?对于数A和B,如果AB在一起组成的数小于BA组成的数,我们就认为A<B,反之亦然。

 public static String minNumber(int[] nums) {
if (nums == null || nums.length == )
return ""; String[] strs = new String[nums.length];
for (int i = ; i < nums.length; i++) {
strs[i] = String.valueOf(nums[i]);
} Arrays.sort(strs, new Comparator<String>() {
public int compare(String str1, String str2) {
return (str1 + str2).compareTo(str2 + str1);
}
}); StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.append(str);
}
for (int i = ; i < sb.length(); i++) {
if (sb.charAt(i) != '') {
return sb.substring(i);
}
}
return "";
}

Reorder array to construct the minimum number的更多相关文章

  1. Minimum number of swaps required to sort an array

    https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unor ...

  2. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  3. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  4. [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...

  5. [LeetCode] Minimum Number of K Consecutive Bit Flips 连续K位翻转的最小次数

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...

  6. Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  7. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. 【leetcode】995. Minimum Number of K Consecutive Bit Flips

    题目如下: In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) suba ...

  9. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

随机推荐

  1. Python之路【第十二篇续】jQuery案例详解

    jQuery 1.jQuery和JS和HTML的关系 首先了HTML是实际展示在用户面前的用户可以直接体验到的,JS是操作HTML的他能改变HTML实际展示给用户的效果! 首先了解JS是一门语言,他是 ...

  2. iOS 加急申请每个开发者必须会

    加急申请原来做过很多次,有成功,有拒绝(最终还是成功,一次不行,被拒绝后多来几下即可,直到成功).但是听朋友说了一件事情,很是不解:他们希望能快速审核上线,在淘宝里面找加速商店,首次上线12000元, ...

  3. cas单点登录 SSO 的实现原理

    原文出处: cutesource   欢迎分享原创到伯乐头条 单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户 ...

  4. Apache CXF实现WebService入门教程(附完整源码)

    Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...

  5. Android Studio-设置放大代码编辑区

    原有的快捷键是ctrl+shift+F12,现在我修改成了Ctrl+M.

  6. vim ctags使用方法

    一.用好系统自带软件ctags 大部分的unix系统都有ctags软件,它能跟vim很好地合作. 用途:     生成c语言的标签文件,实现相关c文件之间的跳转. 用法:     1.生成标签文件   ...

  7. php 选择排序法

    private function demo(array $arr = array()){ $len = count($arr); if ($len == 1) return $arr; else fo ...

  8. Mysql中mysqldump命令使用详解

    MySQL有很多可以导入数据的方法,然而这些只是数据传输中的一半,另外的一般是从MySQL数据库中导出数据.有许多的原因我们需要导出数据.一个重要的原因是用于备份数据库.数据的造价常常是昂贵的,需要谨 ...

  9. codevs5164 逆波兰表达式

    题目描述 Description 逆波兰表达式是一种把运算符前置的算术表达式(又叫前缀表达式),例如普通的表达式2 + 3的逆波兰表示法为+ 2 3.逆波兰表达式的优点是运算符之间不必有优先级关系,也 ...

  10. cocos2d-x内存管理(见解)

    cocos2d-x 延续了cocos2d 和OC的引用计数的内存管理机制! 下面我们来看看CCDriectro类 CCPoolManager::sharedPoolManager()->push ...