当前位置:网站首页>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
边栏推荐
- 为什么 BI 软件都搞不定关联分析
- [the road of Exile - Chapter 4]
- Process -- user address space and kernel address space
- [7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]
- [the road of Exile - Chapter 8]
- 剑指offer专项突击版第13天
- [understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
- 【流放之路-第二章】
- [网鼎杯 2020 朱雀组]Nmap
- 【golang】使用select {}
猜你喜欢
![[the road of Exile - Chapter 5]](/img/ef/7ecc1cb4a95c613f7be91f7acc761c.png)
[the road of Exile - Chapter 5]
![Golang run times undefined error [resolved]](/img/9b/3379aeeff59b47531fe277f7422ce7.png)
Golang run times undefined error [resolved]

数学建模——带相变材料的低温防护服御寒仿真模拟

Have you ever encountered the situation that the IP is blocked when crawling web pages?

Wonderful use of data analysis

Network security litigation risk: four issues that chief information security officers are most concerned about

Stonedb invites you to participate in the open source community monthly meeting!

数学建模——派出所选址

How companies make business decisions -- with the help of data-driven marketing

golang run时报undefined错误【已解决】
随机推荐
覆盖接入2w+交通监测设备,EMQ为深圳市打造交通全要素数字化新引擎
How to find the right agent type? Multi angle analysis for you!
Yocto project download and compilation
秘术冬潮烙技能搭配
Slow storage scheme
[7.21-26] code source - [good sequence] [social circle] [namonamo]
[UE4] replay game playback for ue4.26
Regular filtering data learning notes (①)
Make logic an optimization example in sigma DSP - data distributor
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
为什么 BI 软件都搞不定关联分析
数学建模——自来水管道铺设问题
leetcode/乘积小于K 的连续子数组的个数
[网鼎杯 2020 朱雀组]Nmap
Day01作业
【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
移动通信——基于卷积码的差错控制系统仿真模型
Lua log implementation -- print table
Large scale web crawling of e-commerce websites (Ultimate Guide)
Data platform data access practice