# 179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

Example 1:

``````Input: [10,2]
Output: "210"
``````

Example 2:

``````Input: [3,30,34,5,9]
Output: "9534330"
``````

Note: The result may be very large, so you need to return a string instead of an integer.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

``````class Solution {
public:
string largestNumber(vector<int>& nums) {
string res;
sort(nums.begin(), nums.end(), [](int a, int b) {
});
for (int i = 0; i < nums.size(); ++i) {
res += to_string(nums[i]);
}
return res[0] == '0' ? "0" : res;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/179

https://leetcode.com/problems/largest-number/

https://leetcode.com/problems/largest-number/discuss/53158/My-Java-Solution-to-share

https://leetcode.com/problems/largest-number/discuss/53157/A-simple-C%2B%2B-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation