当前位置:网站首页>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)
边栏推荐
- 微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
- One plus six brushes into Kali nethunter
- 当产业互联网时代真正发展完善之后,将会在每一个场景见证巨头的诞生
- DOM basic syntax
- "2022" is a must know web security interview question for job hopping
- 如果消费互联网比喻成「湖泊」的话,产业互联网则是广阔的「海洋」
- Game 280 of leetcode week
- Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
- Redis' hyperloglog as a powerful tool for active user statistics
- Include rake tasks in Gems - including rake tasks in gems
猜你喜欢

Five ways to query MySQL field comments!

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

增量备份 ?db full
![[OpenGL learning notes 8] texture](/img/77/a4a784a535ea6f4c2382857b266cec.jpg)
[OpenGL learning notes 8] texture

微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益

Blue Bridge Cup Square filling (DFS backtracking)

Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide

微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化

Yyds dry inventory swagger positioning problem ⽅ formula

Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
随机推荐
ROS command line tool
【CTF】AWDP总结(Web)
Win: add general users to the local admins group
How to build a technical team that will bring down the company?
Incremental backup? db full
phpstrom设置函数注释说明
MySQL regexp: Regular Expression Query
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
Wechat applet: wechat applet source code download new community system optimized version support agent member system function super high income
Redis(1)之Redis简介
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!
Database postragesq BSD authentication
How to safely eat apples on the edge of a cliff? Deepmind & openai gives the answer of 3D security reinforcement learning
Win: enable and disable USB drives using group policy
Unified blog writing environment
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Runc hang causes the kubernetes node notready
Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
Robley's global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?