当前位置:网站首页>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)
边栏推荐
- "2022" is a must know web security interview question for job hopping
- [Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
- Unified blog writing environment
- The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
- Nebula importer data import practice
- DOM basic syntax
- 如果消费互联网比喻成「湖泊」的话,产业互联网则是广阔的「海洋」
- batchnorm.py这个文件单GPU运行报错解决
- Express routing, express middleware, using express write interface
- Yyds dry inventory swagger positioning problem ⽅ formula
猜你喜欢
Exploration and practice of integration of streaming and wholesale in jd.com
Yyds dry inventory swagger positioning problem ⽅ formula
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
MySQL REGEXP:正则表达式查询
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
Win: use shadow mode to view the Desktop Session of a remote user
Restful Fast Request 2022.2.1发布,支持cURL导入
微信小程序:独立后台带分销功能月老办事处交友盲盒
One plus six brushes into Kali nethunter
线上故障突突突?如何紧急诊断、排查与恢复
随机推荐
【CTF】AWDP总结(Web)
MATLB | multi micro grid and distributed energy trading
流批一体在京东的探索与实践
Interpretation of mask RCNN paper
Win:使用 PowerShell 检查无线信号的强弱
力扣剑指offer——二叉树篇
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Global and Chinese markets for stratospheric UAV payloads 2022-2028: Research Report on technology, participants, trends, market size and share
How to build a technical team that will bring down the company?
Database postragesq role membership
Introduction to the gtid mode of MySQL master-slave replication
Global and Chinese market of optical densitometers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
Express routing, express middleware, using express write interface
無心劍英譯席慕容《無怨的青春》
【LeetCode】88. Merge two ordered arrays
ROS command line tool
Hedhat firewall
Redis(1)之Redis简介
Database postragesql client authentication