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.

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,反之亦然。

 1 public static String minNumber(int[] nums) {
 2     if (nums == null || nums.length == 0)
 3         return "";
 4
 5     String[] strs = new String[nums.length];
 6     for (int i = 0; i < nums.length; i++) {
 7         strs[i] = String.valueOf(nums[i]);
 8     }
 9
10     Arrays.sort(strs, new Comparator<String>() {
11         public int compare(String str1, String str2) {
12             return (str1 + str2).compareTo(str2 + str1);
13         }
14     });
15
16     StringBuilder sb = new StringBuilder();
17     for (String str : strs) {
18         sb.append(str);
19     }
20     for (int i = 0; i < sb.length(); i++) {
21         if (sb.charAt(i) != ‘0‘) {
22             return sb.substring(i);
23         }
24     }
25     return "0";
26 }
时间: 2024-08-10 14:02:17

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 unordered array consisting of consecutive integers  [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find t

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. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the resu

Leetcode: Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

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. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the resu

Lintcode205 Interval Minimum Number solution 题解

[题目描述] Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers[start, end]. For each query, calculate the minimum number between index start and end in the given array, return th

【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) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips

[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 of length?K?and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of?K-bit flips requir

1326. Minimum Number of Taps to Open to Water a Garden

There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer arr

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s