当前位置:网站首页>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)
边栏推荐
- DOM basic syntax
- Global and Chinese markets of radiation linear accelerators 2022-2028: Research Report on technology, participants, trends, market size and share
- 流批一体在京东的探索与实践
- Complex, complicated and numerous: illustration of seven types of code coupling
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
- Valentine's Day flirting with girls to force a small way, one can learn
- Database postragesq PAM authentication
- Armv8-a programming guide MMU (3)
- Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
猜你喜欢

What sparks can applet container technology collide with IOT

Four pits in reentrantlock!

Yyds dry goods inventory kubernetes management business configuration methods? (08)

JS implementation determines whether the point is within the polygon range

微信小程序:全网独家小程序版本独立微信社群人脉

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Win:使用 Shadow Mode 查看远程用户的桌面会话

【CTF】AWDP总结(Web)

Redis(1)之Redis简介

微信小程序:全新独立后台月老办事处一元交友盲盒
随机推荐
微信小程序:独立后台带分销功能月老办事处交友盲盒
I use these six code comparison tools
Kibana installation and configuration
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
Application and Optimization Practice of redis in vivo push platform
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Basic operations of database and table ----- create index
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
What sparks can applet container technology collide with IOT
[development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
线上故障突突突?如何紧急诊断、排查与恢复
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
Database postragesq peer authentication
Armv8-a programming guide MMU (3)
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
Common bit operation skills of C speech
One click generation and conversion of markdown directory to word format