当前位置:网站首页>[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);
}
};
边栏推荐
- The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
- Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
- 暂停更新公告—行走的皮卡丘
- 实现领域驱动设计 - 使用ABP框架 - 通用准则
- Latex编译成功但是无法输出到PDF
- [tool C] - lattice simulation test 2
- An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
- 【ESP8266-01s】獲取天氣,城市,北京時間
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...
- esp8266-01s 不能连接华为路由器解决方法
猜你喜欢

Wiley-中国科学院文献情报中心开放科学联合研讨会第二讲:开放获取期刊选择及论文投稿...

【ESP8266-01s】获取天气,城市,北京时间

What does the science and technology interactive sand table gain popularity by virtue of
![[esp8266-01s] get weather, city, Beijing time](/img/8f/89e6f0d482f482ed462f1ebd53616d.png)
[esp8266-01s] get weather, city, Beijing time

建立自己的网站(13)

Redis 集群

科技互动沙盘是凭借什么收获人气的

全局组织结构控制之抢滩登陆
![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

Redis Cluster
随机推荐
Go unit test
暂停更新公告—行走的皮卡丘
SimpleDateFormat在多线程环境下存在线程安全问题。
. Net cloud native architect training camp (responsibility chain mode) -- learning notes
Latex编译成功但是无法输出到PDF
csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
3000帧动画图解MySQL为什么需要binlog、redo log和undo log
7、VLAN-Trunk
论文阅读 (53):Universal Adversarial Perturbations
How to solve the problem that the esp8266-01s cannot connect to Huawei routers
An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...
Company offensive operation guide
Skills that all applet developers should know: applying applet components
Wiley- Open Science Joint Symposium of the documentation and information center of the Chinese Academy of Sciences, lecture 2: open access journal selection and paper submission
Implementing Domain Driven Design - using ABP framework - General guidelines
Kotlin practical skills you should know
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
Also using copy and paste to create test data, try the data assistant!
Deploy LNMP environment and install Typecho blog