当前位置:网站首页>179. Maximum number - sort
179. Maximum number - sort
2022-07-05 01:42:00 【The_ Dan】
Given a set of nonnegative integers nums, Rearrange the order of each number ( Each number cannot be split ) Make it the largest integer .
Be careful : The output can be very large , So you need to return a string instead of an integer .
Example 1:
Input :nums = [10,2]
Output :“210”
Example 2:
Input :nums = [3,30,34,5,9]
Output :“9534330”
Example 3:
Input :nums = [1]
Output :“1”
Example 4:
Input :nums = [10]
Output :“10”
Nature is n Order of elements , As for which one comes first , Which one is in the back , Just add the character form to compare , for example "3"+“30" And "30”+"3" Compare , It can be concluded that 3 Put it in front .
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string> vecStr;
for(auto num : nums){
vecStr.push_back(to_string(num));
}
for(auto i = 0; i != vecStr.size(); ++i){
for(auto j = i + 1; j != vecStr.size(); ++j){
string str1 = vecStr[i] + vecStr[j], str2 = vecStr[j] + vecStr[i];
if(str1 < str2)
swap(vecStr[i], vecStr[j]);
}
}
string ans;
for(auto str : vecStr)
ans += str;
if(ans[0] == '0') // Exists in the sample [0,0] The situation of , In this way, we will output "00", It should be "0"
return "0";
return ans;
}
};
Accepted
230/230 cases passed (12 ms)
Your runtime beats 36.26 % of cpp submissions
Your memory usage beats 27.96 % of cpp submissions (11.1 MB)
边栏推荐
- Expansion operator: the family is so separated
- Summary of regularization methods
- Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
- MySQL backup and recovery + experiment
- Interesting practice of robot programming 15- autoavoidobstacles
- Application and Optimization Practice of redis in vivo push platform
- One plus six brushes into Kali nethunter
- 如何搭建一支搞垮公司的技術團隊?
- One plus six brushes into Kali nethunter
- Phpstrom setting function annotation description
猜你喜欢
Exploration and Practice of Stream Batch Integration in JD
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
力扣剑指offer——二叉树篇
Async/await you can use it, but do you know how to deal with errors?
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Incremental backup? db full
ROS command line tool
Main window in QT application
Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
随机推荐
To sort out messy header files, I use include what you use
Redis' hyperloglog as a powerful tool for active user statistics
Heartless sword English translation of Xi Murong's youth without complaint
Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Basic operations of database and table ----- create index
MySQL backup and recovery + experiment
MySQL REGEXP:正则表达式查询
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
Exploration and Practice of Stream Batch Integration in JD
流批一体在京东的探索与实践
Codeforces Global Round 19 ABC
Redis(1)之Redis简介
Can financial products be redeemed in advance?
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
Phpstrom setting function annotation description
Huawei machine test question: longest continuous subsequence
142. Circular linked list II
Jcenter () cannot find Alibaba cloud proxy address