Skip to content

Latest commit

 

History

History
35 lines (34 loc) · 784 Bytes

179.md

File metadata and controls

35 lines (34 loc) · 784 Bytes

cpp

  • 4ms
  • 98%
class Solution {
public:
    string largestNumber(vector<int>& nums) {
        if(all_of(nums.begin(), nums.end(), [](int num){return num==0;})){return "0";}
        vector<string> arr;
        for(auto& val: nums){
            arr.push_back(to_string(val));
        }
        sort(arr.begin(), arr.end(), [](string& s1, string& s2){return s1+s2>s2+s1;});
        string ans;
        for(auto& val: arr){
            ans += val;
        }
        return ans;
    }
};

python2

  • 20ms
  • 94%
class Solution(object):
    def largestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        ans = "".join(sorted(map(str, nums),lambda x,y: [1,-1][x+y>y+x]))
        return ans if ans[0]!='0' else "0"