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 ...
随机推荐
- iOS-马上着手开发iOS应用应用程序-第二部分构建应用程序
第二部分构建应用程序 1,应用程序开发过程 2,设计用户界面 3,定义交互 4,教程:串联图 1,应用程序开发过程 定义概念 设计用户界面 定义交互 实现行为整合数据 对象是应用程序的基石 类是对象的 ...
- 记录两张数据库表及Ibatis操作
建表语句 CREATE TABLE `TS_MopayInvoiceComposition` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `RequestID` i ...
- CSS实现底部固定
html代码结构 <body> <div class="wrap"> <div class="head"></div& ...
- PHP常用的一些正则表达式
附一些常用的正则运算: 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...
- 基于密度聚类的DBSCAN和kmeans算法比较
根据各行业特性,人们提出了多种聚类算法,简单分为:基于层次.划分.密度.图论.网格和模型的几大类. 其中,基于密度的聚类算法以DBSCAN最具有代表性. 场景 一 假设有如下图的一组数据, 生成数据 ...
- Python学习手册(1入门知识-数据类型)
UNIX env查找技巧 在一些UNIX系统上,可以用这样一种方法避免硬编码Python解释器的路径,在文件的特定的第一行注释中写上这样一句话. #! usr/bin/env/ python...sc ...
- MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解
本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍,需要的朋友参考下 MySQL数据库insert和update语句引:用于操作数 ...
- 利用PHP读取文件
$fp=fopen("D:\\phpStudy\\www\\date\\file\\2.txt","r");if($fp){ while(!feof($f ...
- 如何自定义wordpress登录界面的Logo
每次登录wp后台都会看到wordpress的logo,会不会有点烦呢?想不想换个新的.自己设定一个呢?那么如何自定义wordpress登录界面的Logo呢? 把代码复制到当前主题的 functions ...
- PHP中PSR-[0-4]代码规范
PHP-FIG 在说啥是PSR-[0-4]规范的之前,我觉得我们有必要说下它的发明者和规范者:PHP-FIG,它的网站是:www.php-fig.org.就是这个联盟组织发明和创造了PSR-[0-4] ...