个人主页:Lei宝啊
愿所有美好如期而遇
本题链接https://leetcode.cn/problems/largest-number/description/
class Solution {
public:bool static compare(int a, int b){return (to_string(a) + to_string(b)) > (to_string(b) + to_string(a));}bool operator()(int a, int b){return (to_string(a) + to_string(b)) > (to_string(b) + to_string(a));}string largestNumber(vector<int>& nums) {sort(nums.begin(), nums.end(),Solution());//sort(nums.begin(), nums.end(),compare);string s;for(auto e : nums){s += to_string(e);}if(s[0] == '0')return "0";return s;}
};
按照我们正常对一个数组排序,就是使得一个数到他正确的位置上,本题我们要排的序,本质上也是使得每个数到他正确的位置上,不过只是排序的方法变了而已,现在的排序方式变成了
to_string(a) + to_string(b) > to_string(b) + to_string(a),以此来判断到底是哪个数在前,哪个数在后。