当前位置:网站首页>剑指 Offer 笔记: T45. 把数组排成最小的数
剑指 Offer 笔记: T45. 把数组排成最小的数
2022-07-27 10:58:00 【无知小九】
T45. 把数组排成最小的数
输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
示例 1:
输入: [10,2]
输出: “102”
示例 2:
输入: [3,30,34,5,9]
输出: “3033459”
提示:
0 < nums.length <= 100
说明:
输出结果可能非常大,所以你需要返回一个字符串而不是整数
拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0
解法 1
没做出来
解法 2
class Solution {
public String minNumber(int[] nums) {
List<String> strList = new ArrayList<>();
for (int num : nums) {
strList.add(String.valueOf(num));
}
strList.sort((a, b) -> (a + b).compareTo(b + a));
StringBuilder sb = new StringBuilder();
for (String str : strList) {
sb.append(str);
}
return sb.toString();
}
}
作者:画手大鹏
链接:https://leetcode-cn.com/leetbook/read/illustrate-lcof/o585xr/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Inclusion exclusion principle acwing 890. divisible numbers
- torch‘ has no attribute ‘inference_mode‘
- Lazy loading of lists and pictures
- 第8章 多线程
- 你真的会写二分查找吗——变种二分查找
- C programming language (2nd Edition) -- Reading Notes -- 1.5.2
- (3) Pass parameters
- 本地虚拟机初始化脚本
- [shader realizes shake random shaking effect _shader effect Chapter 10]
- 2022 Niuke multi school training (3) a-ancestor topic translation
猜你喜欢

第12章 泛型

Properties file

博弈论 AcWing 894. 拆分-Nim游戏

Game theory acwing 893. Set Nim game

Moveit2 -- 2. Quick start of moveit in rviz

记忆化搜索 AcWing 901. 滑雪

Interval problem acwing 906. Interval grouping

Digital triangle model acwing 1015. Picking flowers

求组合数 AcWing 886. 求组合数 II

Stack acwing 3302. Expression evaluation
随机推荐
Longest ascending subsequence model acwing 1014. mountaineering
What is private traffic?
为什么选择智能电视?
349 sum of intersection of two arrays and 01
Stm32f10x -- C Language-1
Force buckle - 10. Regular expression matching
(4) Operator
Moveit2 -- 2. Quick start of moveit in rviz
Basic use of cmake
(8) Shell function
多家银行调整现金管理类理财产品申赎规则:申赎确认时效“T+0”变“T+1”
Backpack model acwing 1022. Collection of pet elves
【着色器实现Shake随机摇动效果_Shader效果第十篇】
Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
剑指 Offer 笔记: T58 - II. 左旋转字符串
Digital triangle model acwing 1018. Minimum toll
Everything cannot be searched for startup_ Lpc11x.s file
多种进制之间的转换
数字三角形模型 AcWing 1015. 摘花生
Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage