当前位置:网站首页>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)
边栏推荐
- To sort out messy header files, I use include what you use
- Jcenter () cannot find Alibaba cloud proxy address
- JS implementation determines whether the point is within the polygon range
- Unified blog writing environment
- Interpretation of mask RCNN paper
- Express routing, express middleware, using express write interface
- PowerShell: use PowerShell behind the proxy server
- Common bit operation skills of C speech
- batchnorm. Py this file single GPU operation error solution
- Es uses collapsebuilder to de duplicate and return only a certain field
猜你喜欢
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
Nebula importer data import practice
runc hang 导致 Kubernetes 节点 NotReady
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
Exploration and Practice of Stream Batch Integration in JD
Four pits in reentrantlock!
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
How to safely eat apples on the edge of a cliff? Deepmind & openai gives the answer of 3D security reinforcement learning
Game 280 of leetcode week
随机推荐
Introduction to the gtid mode of MySQL master-slave replication
PowerShell: use PowerShell behind the proxy server
"2022" is a must know web security interview question for job hopping
La jeunesse sans rancune de Xi Murong
流批一體在京東的探索與實踐
Change the background color of a pop-up dialog
Complex, complicated and numerous: illustration of seven types of code coupling
Heartless sword English translation of Xi Murong's youth without complaint
力扣剑指offer——二叉树篇
Expansion operator: the family is so separated
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Great God developed the new H5 version of arXiv, saying goodbye to formula typography errors in one step, and mobile phones can also easily read literature
Robley's global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Main window in QT application
runc hang 导致 Kubernetes 节点 NotReady
Win: add general users to the local admins group
Classification of performance tests (learning summary)