当前位置:网站首页>【剑指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);
}
};
边栏推荐
- Troubleshooting and modification process of easycvr interface dislocation in small screen
- Nodejs implements multi process
- The principle of MySQL index algorithm and the use of common indexes
- Reinforcement learning series (I) -- basic concepts
- Vulnerability in McAfee epolicy orchestrator
- Three functional forms of intelligent switch
- How to use R language to draw scatter diagram
- Which securities company is good for opening a mobile account? Is online account opening safe?
- How to open an account through online stock? Is online account opening safe?
- Goframe framework: add tracing Middleware
猜你喜欢

MySQL transaction and its characteristics and locking mechanism

Ctfshow PHP features

视频异常检测数据集 (ShanghaiTech)

FPN characteristic pyramid network

酒店入住时间和离店时间的日期选择

论文阅读 (53):Universal Adversarial Perturbations

Wechat applet: time selector for the estimated arrival date of the hotel

MySQL事务及其特性与锁机制

qYKVEtqdDg

论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
随机推荐
Installation, configuration, désinstallation de MySQL
MySQL installation, configuration and uninstall
Vulnerability in McAfee epolicy orchestrator
B. Integers Shop-Hello 2022
Goframe framework: basic auth Middleware
How to design a seckill system?
How to create a three elimination game
如何通过线上股票开户?在线开户安全么?
Intelligent supply chain collaborative management solution for logistics industry
10分钟后性能测试瓶颈调优!想进大厂这个必须会
B. AND 0, Sum Big-Codeforces Round #716 (Div. 2)
Importance of ERP management system
Troubleshooting and modification process of easycvr interface dislocation in small screen
Baidu AI Cloud product upgrade Observatory in May
How about stock online account opening and account opening process? Is online account opening safe?
Kdevtmpfsi processing of mining virus -- Practice
浅析3种电池容量监测方案
[Hyperf]Entry “xxxInterface“ cannot be resolved: the class is not instantiable
视频异常检测数据集 (ShanghaiTech)
PostgreSQL series articles -- the world's most advanced open source relational database