当前位置:网站首页>[sword finger offer] 45 Arrange the array into the smallest number
[sword finger offer] 45 Arrange the array into the smallest number
2022-06-23 18:15:00 【LuZhouShiLi】
The finger of the sword Offer 45. Make the array the smallest number
subject
Enter an array of nonnegative integers , Put all the numbers in the array together to form a number , Print the smallest of all the numbers that can be spliced .
Ideas
String sorting problem , Set array nums The string of any two numbers in is x and y, The sorting rule is :
- x + y > y + x, be x Greater than y
- x + y < y + x, be x Less than y
Then convert the list of numbers to an array of strings , Use quick sort to sort an array of strings ,( The main purpose of sorting is to make the high-order numbers as small as possible , Big numbers are placed in the lower order ), Finally, the strings in the string array are spliced into a numeric string .
Code
class Solution {
public:
string minNumber(vector<int>& nums) {
vector<string> strs;
// Convert a number to a string and save
for(int i = 0; i < nums.size(); i++)
{
strs.push_back(to_string(nums[i]));
}
// Quick sort
quickSort(strs,0,strs.size() - 1);
// Put the smallest combination Put in string
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)
{
// Compare string sizes find jl < lj stop it
// Why and l because l Is the largest digit of a number !
while(strs[j] + strs[l] >= strs[l] + strs[j] && i < j)
{
j--;
}
// il > li stop it Find the one with a large value Put it back
while(strs[i] + strs[l] <= strs[l] + strs[i] && i < j)
{
i++;
}
// In exchange for Make the high-order number smaller The lower digit becomes larger Overall smaller
swap(strs[i],strs[j]);
}
swap(strs[i],strs[l]);
quickSort(strs,l,i - 1);
quickSort(strs,i + 1, r);
}
};
边栏推荐
- Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
- 【华中科技大学】考研初试复试资料分享
- Ner's past, present and future Overview - Future
- 论文阅读 (49):Big Data Security and Privacy Protection (科普文)
- Establishment and use of SSL VPN (OpenVPN)
- 论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
- Strong cache and negotiation cache in http
- QML type: Loader
- Redis Cluster
- 13. IP address and subnet partitioning (VLSM)
猜你喜欢

Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints

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

提高效率 Or 增加成本,开发人员应如何理解结对编程?

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...

Paper reading (50):a novel matrix game with payoffs of maximal belt structure

torch学习(一):环境配置

Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
![Wechat applet reports an error [app.json file content error] app json: app. JSON not found](/img/ab/5c27e1bb80ad662d1a220d29c328e0.png)
Wechat applet reports an error [app.json file content error] app json: app. JSON not found

The battlefield of live broadcast e-commerce is not in the live broadcast room

全局组织结构控制之抢滩登陆
随机推荐
Lighthouse open source application practice: o2oa
[win10 vs2019 opencv4.6 configuration reference]
如何利用好每天的时间高效复习?
科技互动沙盘是凭借什么收获人气的
2021 excellent TDP members' output data summary is coming, come and watch!!!
【win10 VS2019 opencv4.6 配置参考】
信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
A set of code to launch seven golang web frameworks at the same time
建立自己的网站(13)
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
Redis Cluster
What does the science and technology interactive sand table gain popularity by virtue of
Kerberoasting without SPN
ACM players take you to play with the array!
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found):...
console. Log() is an asynchronous operation???
Goframe framework: add tracing Middleware
对抗攻击与防御 (2):对抗样本的反制策略
论文阅读 (49):Big Data Security and Privacy Protection (科普文)
【华中科技大学】考研初试复试资料分享