当前位置:网站首页>【剑指Offer】45. 把数组排成最小的数
【剑指Offer】45. 把数组排成最小的数
2022-06-23 16:50:00 【LuZhouShiLi】
剑指 Offer 45. 把数组排成最小的数
题目
输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
思路
字符串排序问题,设数组nums中任意两个数字的字符串为x和y,则排序的判断规则是:
- x + y > y + x,则x大于y
- x + y < y + x, 则x 小于y
然后将数字列表转换为字符串数组,使用快速排序对字符串数组进行排序,(排序的主要目的其实就是将高位的数字尽可能变小,大的数字放在低位上),最后将字符串数组中的字符串拼接成一个数字字符串。
代码
class Solution {
public:
string minNumber(vector<int>& nums) {
vector<string> strs;
// 将数字转化为字符串并保存
for(int i = 0; i < nums.size(); i++)
{
strs.push_back(to_string(nums[i]));
}
// 快速排序
quickSort(strs,0,strs.size() - 1);
// 将最小的组合 放进字符串
string res;
for(string s:strs)
{
res.append(s);
}
return res;
}
private:
void quickSort(vector<string>& strs,int l,int r)
{
if(l >= r)
{
return;
}
int i = l,j = r;
while(i < j)
{
// 比较字符串大小 找到jl < lj停止
// 为什么和l 因为l是数字的最大位!
while(strs[j] + strs[l] >= strs[l] + strs[j] && i < j)
{
j--;
}
// il > li 停止 找到数值大的 放后面
while(strs[i] + strs[l] <= strs[l] + strs[i] && i < j)
{
i++;
}
// 交换 使得高位数字变小 低位数字变大 总体变小
swap(strs[i],strs[j]);
}
swap(strs[i],strs[l]);
quickSort(strs,l,i - 1);
quickSort(strs,i + 1, r);
}
};
边栏推荐
- What is the personal finance interest rate in 2022? How do individuals choose financial products?
- Goframe framework: add tracing Middleware
- Hapoxy-集群服务搭建
- Deploy LNMP environment and install Typecho blog
- POC about secureworks' recent azure Active Directory password brute force vulnerability
- [learning notes] tidb learning notes (III)
- CRMEB 二开短信功能教程
- Wechat applet: time selector for the estimated arrival date of the hotel
- Three functional forms of intelligent switch
- B. AND 0, Sum Big-Codeforces Round #716 (Div. 2)
猜你喜欢
![[30. concatenate substrings of all words]](/img/e7/453c8524a23fbb7501e85140547ce1.png)
[30. concatenate substrings of all words]

论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints

微信小程序:酒店预计到店日期的时间选择器

qYKVEtqdDg

README

MySQL事务及其特性与锁机制

论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...

Hands on data analysis unit 2 section 4 data visualization

Alien world, real presentation, how does the alien version of Pokemon go achieve?

酒店入住时间和离店时间的日期选择
随机推荐
美团三面:聊聊你理解的Redis主从复制原理?
Illustration of mongodb cluster deployment principle (3)
C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
如何通过线上股票开户?在线开户安全么?
Detailed explanation of ssl/tls principle and packet capturing
Analysis of three battery capacity monitoring schemes
浅析3种电池容量监测方案
New function! Qianfan magic pen apaas December capability monthly report
Baidu AI Cloud product upgrade Observatory in May
Performance test bottleneck tuning in 10 minutes! If you want to enter a large factory, you must know
Troubleshooting and modification process of easycvr interface dislocation in small screen
How do I write a small program that can automatically edit new year greetings
Kerberoasting without SPN
What if the website is poisoned
Also using copy and paste to create test data, try the data assistant!
Kdevtmpfsi processing of mining virus -- Practice
Meituan Sanmian: how do you understand the principle of redis master-slave replication?
12 initialization of beautifulsoup class
FPN characteristic pyramid network
Read the typical application circuit of microphone