当前位置:网站首页>【剑指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);
}
};
边栏推荐
- Answer 01: why can Smith circle "allow left string and right parallel"?
- 论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints
- What is the personal finance interest rate in 2022? How do individuals choose financial products?
- 解答02:Smith圓為什麼能“上感下容 左串右並”?
- Cross browser common events
- First use of kubernetes cronjob
- Tencent three sides: how to duplicate 4billion QQ numbers?
- Ner's past, present and future Overview - Future
- 论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
- Easygbs playback screen is continuously loading. Troubleshooting
猜你喜欢

论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...

Meituan Sanmian: how do you understand the principle of redis master-slave replication?

论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks

MySQL事务及其特性与锁机制

Wechat applet: time selector for the estimated arrival date of the hotel
![[30. concatenate substrings of all words]](/img/e7/453c8524a23fbb7501e85140547ce1.png)
[30. concatenate substrings of all words]

JSON - learning notes (message converter, etc.)

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

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design

论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)
随机推荐
Three functional forms of intelligent switch
Postgresql_ Optimize SQL based on execution plan
What is the mobile account opening process? Is it safe to open an account online now?
解析 | 模-数(A/D)转换器
How to design a seckill system - geek course notes
Latex编译成功但是无法输出到PDF
Answer 02: why can Smith circle "allow left string and right parallel"?
PostgreSQL series articles -- the world's most advanced open source relational database
全局组织结构控制之抢滩登陆
How to make validity table
How to use R language to draw scatter diagram
Single fire wire design series article 10: expanding application - single fire switch realizes double control
解答02:Smith圓為什麼能“上感下容 左串右並”?
Company offensive operation guide
百度智能云5月产品升级观察站
High availability solution practice of mongodb advanced applications (4)
《AN4190应用笔记 天线选择指南》——天线理论2
Cross browser common events
How to quickly obtain and analyze the housing price in your city?
Importance of ERP management system