当前位置:网站首页>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
边栏推荐
- [UE4] replay game playback for ue4.26
- 【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
- 抓包工具Charles使用
- [golang] use select {}
- [7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
- Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
- Add graceful annotations to latex formula; "Data science" interview questions collection of RI Gai; College Students' computer self-study guide; Personal firewall; Cutting edge materials / papers | sh
- 数学建模——自来水管道铺设问题
- E-commerce keyword research helps data collection
- 【流放之路-第六章】
猜你喜欢
![[golang] network connection net.dial](/img/8d/7ef64cb63cbd230e5ac1655b86786f.png)
[golang] network connection net.dial

Minimalist thrift+consumer

druid. io kill -9 index_ Realtime traceability task

Yocto project download and compilation

Dynamic memory and smart pointer

As long as I run fast enough, it won't catch me. How does a high school student achieve a 70% salary increase under the epidemic?

Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years

【GoLang】同步锁 Mutex

Data platform data access practice

Six noteworthy cloud security trends in 2022
随机推荐
【golang】使用select {}
Sword finger offer special assault edition day 13
Overview of Qualcomm 5g intelligent platform
Analyze OP based on autoware_ global_ Planner global path planning module re planning
[UE4] replay game playback for ue4.26
抓包工具Charles使用
规划数学期末模拟考试一
【流放之路-第五章】
Some summaries of ibatis script and provider
Lua third-party byte stream serialization and deserialization module --lpack
Use of packet capturing tool Charles
数学建模——自来水管道铺设问题
ciscn 2022 华中赛区 misc
数学建模——红酒品质分类
In depth analysis of C language memory alignment
[the road of Exile - Chapter 8]
Talk about possible problems when using transactions (@transactional)
JS timer setinterval clearinterval delayer setTimeout asynchronous animation
LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
How to prevent all kinds of affiliated fraud?