3ms C++ solution


  • 0
    T
    class Solution {
    public:
        string largestNumber(vector<int>& nums) {
            vector<string> strNums;
            for (auto num : nums) {
                strNums.push_back(to_string(num));
            }
            sort(strNums.begin(), strNums.end(), [](const string& s1, const string& s2){
                string num1 = s1 + s2, num2 = s2 + s1;
                return num1 > num2;
            });
            string res;
            for (auto num : strNums) {
                res += num;
            }
            bool allZero = true;
            for (auto bit : res) {
                if (bit != '0') {
                    allZero = false;
                    break;
                }
            }
            if (allZero) {
                return "0";
            } else {
                return res;
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.