当前位置:网站首页>【剑指Offer】45. 把数组排成最小的数
【剑指Offer】45. 把数组排成最小的数
2022-06-23 16:50:06【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);
}
};
边栏推荐
- 手机开户一般哪个证券公司好?在线开户安全么?
- 百度智能云5月产品升级观察站
- CRMEB 二开短信功能教程
- torch学习(一):环境配置
- README
- 论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
- 论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
- 论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)
- Latex编译成功但是无法输出到PDF
- 论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints
猜你喜欢
全局组织结构控制之抢滩登陆
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
视频异常检测数据集 (ShanghaiTech)
论文阅读 (53):Universal Adversarial Perturbations
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
对抗攻击与防御 (2):对抗样本的反制策略
论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
console. Log() is an asynchronous operation???
Kdevtmpfsi processing of mining virus -- Practice
随机推荐
- Also using copy and paste to create test data, try the data assistant!
- Async/await
- Programmers are very useful ten tool websites, which are worth collecting
- Troubleshooting and modification process of easycvr interface dislocation in small screen
- Reinforcement learning series (I) -- basic concepts
- Easygbs playback screen is continuously loading. Troubleshooting
- 13. IP address and subnet partitioning (VLSM)
- 12. Manage network environment
- Illustration of mongodb cluster deployment principle (3)
- 6、VLAN
- What if the website is poisoned
- 7、VLAN-Trunk
- Revil - blackmail Virus Emergency Response
- How to make a badge
- Alien world, real presentation, how does the alien version of Pokemon go achieve?
- How to use R language to draw scatter diagram
- How to make validity table
- Ner's past, present and future Overview - Future
- This time, thoroughly understand the SparseArray implementation principle
- Detailed explanation of ssl/tls principle and packet capturing
- Kotlin practical skills you should know
- New function! Qianfan magic pen apaas December capability monthly report
- Nodejs implements multi process
- Goframe framework: basic auth Middleware
- Crmeb second open SMS function tutorial
- Baidu AI Cloud product upgrade Observatory in May
- Which securities company is good for opening a mobile account? Is online account opening safe?
- Kerberoasting without SPN
- Mobile SSH connection tool
- How to make towel washing label
- Establishment and use of SSL VPN (OpenVPN)
- A set of code to launch seven golang web frameworks at the same time
- Vulnerability in McAfee epolicy orchestrator
- Deploy LNMP environment and install Typecho blog
- Cryptography involved in IOT device end
- [learning notes] tidb learning notes (III)
- Goframe framework: add tracing Middleware
- Call face recognition exception
- Self taught programming introduction, what language to learn first?
- How do I write a small program that can automatically edit new year greetings