当前位置:网站首页>[sword finger offer] interview question 45: arrange the array into the smallest number
[sword finger offer] interview question 45: arrange the array into the smallest number
2022-07-27 15:52:00 【Jocelin47】

1. The higher number must be the smaller the better
2. The order of putting numbers must be first ( The leftmost one ) The smallest element , If the first digit is equal , Compare the second …, And so on .
3. If we convert all the numbers into strings and arrange them
4. Compare all strings
5. Two strings s1,s2 If s1+ s2 > s2 + s1 that s1 > s2
such as s1 by 3,s2 by 30, because s1+s2 = 330 ,s2+s1 = 303, therefore s1 > s2
Therefore, we can define string comparison rules by ourselves :
static bool compare(string &s1, string &s2)
{
return s1 + s2 < s2 + s1;
}
adopt lambda Expressions can also be written .
The code is as follows :
class Solution {
public:
string minNumber(vector<int>& nums) {
vector<string> strs;
string result;
for( int i = 0; i < nums.size(); i++)
{
strs.push_back(to_string(nums[i]));
}
sort(strs.begin(), strs.end(), [](string &s1, string &s2) {
return s1 + s2 < s2 + s1;} );
//sort(strs.begin(), strs.end(), compare);
for( int i = 0; i < strs.size(); i++ )
{
result += strs[i];
}
return result;
}
// static bool compare(string &s1, string &s2)
// {
// return s1 + s2 < s2 + s1;
// }
};
Make up the largest number
https://www.nowcoder.com/questionTerminal/fc897457408f4bbe9d3f87588f497729
边栏推荐
- C语言:扫雷小游戏
- [Yunxiang book club issue 13] coding format of video files
- 股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
- Database: use the where statement to retrieve (header song)
- /Dev/loop1 takes up 100% of the problem
- 网络层的IP协议
- Spark RPC
- Is the array name the address of the first element?
- 多线程带来的的风险——线程安全
- [TensorBoard] OSError: [Errno 22] Invalid argument处理
猜你喜欢

Binder initialization process

复杂度分析

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

C language: function stack frame

后台返回来的是这种数据,是什么格式啊

Half find

Troubleshooting the slow startup of spark local programs

【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版

Is low code the future of development? On low code platform

First understanding of structure
随机推荐
初识MySQL数据库
[regular expression] match the beginning and end
Text batch replacement function
JS operation DOM node
After the table is inserted into an in row formula, the cell loses focus
网络原理(1)——基础原理概述
Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
复杂度分析
Go language slow start - Basic built-in types
面试重点——传输层的TCP协议
使用双星号代替Math.pow()
Voice live broadcast system -- a necessary means to improve the security of cloud storage
初识结构体
js使用一元运算符简化字符串转数字
Binder initialization process
Talk about the index of interview questions
Spark 3.0 testing and use
Leetcode-1: sum of two numbers
The method of exchanging two numbers in C language
go语言慢速入门——包