当前位置:网站首页>Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
2022-07-29 02:03:00 【SK_ Jaco】
1. Title Description
The finger of the sword Offer 45. Make the array the smallest number
Enter an array of nonnegative integers , Put all the numbers in the array together to form a number , Print the smallest of all the numbers that can be spliced .
Example 1:
Input : [10,2]
Output : "102"
Example 2:
Input : [3,30,34,5,9]
Output : "3033459"
2. Problem solving ideas and codes
2.1 Their thinking
This question is very simple , Make the numbers in the array as small as possible , Then let the smaller the front row be in front , In this way, the higher the number of digits, the smaller the number . Then we can convert numbers into strings for splicing , After being converted into a string, we are actually comparing whether the lexicographic order after the numbers are spliced is the smallest . Based on this idea , First, we convert a number array into a string list , Then sort the string list , The collation compares the dictionary order after the splicing of two strings , Finally, you can splice the string list and return . With 3 and 30 For example , First, splice the two numbers , There are two splicing methods , The results are 330 and 303
Then compare the dictionary order of two numbers , Because the dictionary order starts from left to right , Just meet the meaning of the topic , So let's compare the first place of the two numbers , The first one is 3 So the next one compares , here 0 The dictionary order of is less than 3 , therefore 303 Than 330 smaller , return 303.

2.2 Code
class Solution {
public String minNumber(int[] nums) {
StringBuilder builder = new StringBuilder();
List<String> list = new ArrayList<>();
for (int num : nums) {
list.add(String.valueOf(num));
}
list.sort((o1, o2) -> {
String s1 = o1 + o2;
String s2 = o2 + o1;
return s1.compareTo(s2);
});
for (String s : list) {
builder.append(s);
}
return builder.toString();
}
}
2.3 test result
Pass the test

3. summary
- Convert a number array into a string list for processing
- Sort the list in dictionary order , Finally, return the list splicing results
边栏推荐
- Process -- user address space and kernel address space
- 使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包
- More interesting Title Dynamic Effect
- 剑指offer专项突击版第13天
- LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
- Introduction to shared data center agent
- 使用本地缓存+全局缓存实现小型系统用户权限管理
- 知道创宇上榜CCSIP 2022全景图多个领域
- Golang startup error [resolved]
- 【MySQL】sql给表起别名
猜你喜欢

StoneDB 邀请您参与开源社区月会!

Wonderful use of data analysis

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen

StoneDB 邀请您参与开源社区月会!
![[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform](/img/e7/1d06eba0e50eeb91d2d5da7524f4af.png)
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform

覆盖接入2w+交通监测设备,EMQ为深圳市打造交通全要素数字化新引擎

Process -- user address space and kernel address space
![[the road of Exile - Chapter 6]](/img/cd/408118f43dbda2e6367b328a1c20fc.png)
[the road of Exile - Chapter 6]

Use POI to export excel file, image URL to export file, image and excel file to export compressed package

druid. IO custom real-time task scheduling policy
随机推荐
JVM learning minutes
【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
为什么 BI 软件都搞不定关联分析
Slow storage scheme
Stonedb invites you to participate in the open source community monthly meeting!
抓包工具Charles使用
[UE4] replay game playback for ue4.26
[the road of Exile - Chapter 5]
【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
Sigma-DSP-OUTPUT
Wonderful use of data analysis
数学建模——永冻土层上关于路基热传导问题
leetcode/0和1个数相同的连续子数组
【流放之路-第八章】
What is a proxy server? [2022 guide]
【Golang】- runtime.Goexit()
【流放之路-第二章】
【golang】使用select {}
【GoLang】同步锁 Mutex
数学建模——公交调度优化