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. Log4cpp配置文件格式说明

    Log4cpp配置文件格式说明 博客分类: log4cpp log4cpp  log4cpp有3个主要的组件:categories(类别).appenders(附加目的地).和 layouts(布局) ...

  2. QQ空间HD(4)-设置左侧菜单栏属性

    DJHomeViewController.m #import "DJHomeViewController.h" #import "DJMenuView.h" ; ...

  3. JSF页面中使用js函数回调后台bean方法并获取返回值的方法

    由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...

  4. dreamwaver的动态相关文件 工具栏搜索

    dreamwaver是很好的编辑工具, 用过很多ide, 对php,js, css代码来说, dw确实是很好很方便的一个工具 php本身设置了很多的 预定义常量, 函数, 可以用来获取当前运行php的 ...

  5. 理解Memcached的分布式

    Memcached尽管是"分布式"的缓存系统,但是服务器端并没有分布式功能.各个Memcached实例不会相互通信以共享信息,Memcached如何进行分布式完全取决于客户端的实现 ...

  6. CF467D Fedor and Essay 建图DFS

      Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...

  7. hdu3729 I'm Telling the Truth (二分图的最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...

  8. Sublime Text 3初阶

    本文主要介绍一些Sublime Text3的初级阶段,主要从最初的安装,到插件,还有主题这三个方面介绍,还会提到一些关于使用ST3的一些小小经验... 一:安装 首先进入sublime的官方地址去下载 ...

  9. getStyle(),修改样式属性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. (免量产,免格式化)手动将PE安装到移动硬盘/U盘或无系统硬盘!

    在一台没有装系统的电脑上,只要把XP系统启动文件,及引导菜单文件(ntldr,boot.ini,”bootfont.bin这个可有可无,主要作用是显示中文菜单”)和GRUB引导文件和PE系统文件复制到 ...