Reorder array to construct the minimum number
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.
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的更多相关文章
- 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 ...
- Leetcode: Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 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. ...
- [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 ...
- [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 ...
- 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. ...
- Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 【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 ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
随机推荐
- Python-面向对象编程(一)
初识面向对象: Python中一切皆对象,我自己,我的电脑,电脑桌,都称之为一个对象.对象是类的一个实体. 我们可以通过行为和特征(属性)来描述一个对象,比如小狗,它有四条腿,一个尾巴,两个虎牙,这就 ...
- [转] MemCached 的 stats 命令
Memcached有个stats命令,通过它可以查看Memcached服务的许多状态信息.使用方法如下:先在命令行直接输入telnet 主机名端口号,连接到memcached服务器,然后再连接成功后, ...
- line-height:150%和 line-height:1.5 的区别
父元素line-height为150%或1.5em时,依据父元素的字体大小计算出行高值让子元素继承父元素line-height为1.5时,依据子元素字体大小计算其行高值.
- jquery取消选择select下拉框
有三个select下拉框一个大类,两个小类隐藏,需要在选择大类的时候,小类显示同时清除另外的小类选择的项这需求有点儿.......... 下面是三个select: <select name=&q ...
- 二、oracle pctfree和pctused详解
一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...
- iOS-设置UIPageControl 显示图片
UIPageControl 的默认样式是几个小圆点,系统没有提供属性供我们自定义这几个小圆点的样式,不过我们依然可以使用KVC来自定义PageControl的显示样式 UIPageControl *p ...
- PHP数据类型转换
转自:http://www.tianzhigang.com/article.asp?id=280 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: (int).(integer):转换成 ...
- shell学习之路:shell基础大全2
原文:http://note.youdao.com/share/?id=cd2ad6e6d5db2b347f92958efc2bdbc1&type=note 正则表达式与通配符: 一.介绍: ...
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...
- 关于R中的mode()和class()的区别
本文原创,转载请注明出处,本人Q1273314690(交流学习) 说明:本文曾经在15年11月在CSDN发过,但是由于CSDN不支持为知笔记的发布为博客的API功能,所以,自今天起,转移到博客园(幸好 ...