当前位置:网站首页>LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
2022-07-29 01:11:00 【SK_Jaco】
1.题目描述
剑指 Offer 45. 把数组排成最小的数
输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
示例 1:
输入: [10,2]
输出: "102"
示例 2:
输入: [3,30,34,5,9]
输出: "3033459"
2.解题思路与代码
2.1 解题思路
这道题其实非常简单,要让数组中的数字拼接起来的数字尽可能小,那么就要让前面位越小的排在与前面,这样保证位数越高的数字越小。那么我们就可以将数字转化成字符串来进行拼接,转换成字符串之后其实我们就是比较数字拼接起来之后的字典序是否最小。基于这样的思想,首先我们将数字数组转换成字符串列表,然后对字符串列表进行排序,排序规则比较两个字符串拼接后的字典序,最后将字符串列表拼接起来返回即可。以 3 和 30 为例,首先对两个数字进行拼接,拼接方式有两种,结果分别是 330 和 303
然后比较两个数的字典序,由于字典序是从左往右开始比较,正好满足题意,因此我们先比较两个数的第一位,第一位都是 3 于是往下一位比较,此时 0 的字典序小于 3 ,因此 303 比 330 更小,返回 303.

2.2 代码
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 测试结果
通过测试

3.总结
- 将数字数组转换成字符串列表进行处理
- 依次按照字典序对列表进行排序,最后返回列表拼接结果即可
边栏推荐
- 科研环境对人的影响是很大的
- The basic concept of transaction and the implementation principle of MySQL transaction
- Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
- [web technology] 1395 esbuild bundler HMR
- DSP vibration seat
- [netding cup 2020 rosefinch group]nmap
- 把逻辑做在Sigma-DSP中的优化实例-数据分配器
- Making high-precision map based on autoware (V)
- [7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
- ciscn 2022 华中赛区 misc
猜你喜欢

【流放之路-第三章】

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!

九天后我们一起,聚焦音视频、探秘技术新发展

What are the common cyber threats faced by manufacturers and how do they protect themselves

数学建模——红酒品质分类

Network security litigation risk: four issues that chief information security officers are most concerned about
![[hcip] MPLS Foundation](/img/91/a2aebf333fb3e3fedf0fc393e175a9.png)
[hcip] MPLS Foundation

Analyze OP based on autoware_ global_ Planner global path planning module re planning

【流放之路-第五章】

【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
随机推荐
Day01作业
Sigma-DSP-OUTPUT
Comprehensive analysis of news capture doorway
Regular filtering data learning notes (①)
Basic label in body
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
[hcip] experiment of republishing and routing strategy
Tda75610-i2c-determination of I2C address of analog power amplifier
Yocto project download and compilation
Explanation of yocto project directory structure
【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
Leetcode 112: path sum
How to protect WordPress website from network attack? It is essential to take safety measures
Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?
Minimalist thrift+consumer
基于 ICA 与 DL 的语音信号盲分离
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
【流放之路-第六章】