Leetcode: Largest Number

来源:互联网 发布:安卓扫雷源码 编辑:程序博客网 时间:2024/06/04 20:29

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

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

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

Solution: 

class Solution {public:static bool cmp(string a, string b) {return a + b > b + a;}string largestNumber(vector<int> &num) {vector<string> snum;for (int i = 0; i < num.size(); i++)snum.push_back(to_string(num[i]));sort(snum.begin(), snum.end(), cmp);string res = "";for (int i = 0; i < snum.size(); i++)res += snum[i];if (res[0] == '0')return "0";return res;}};






0 0
原创粉丝点击