其他分享
首页 > 其他分享> > 2022-5-3 每日一题-leetcode

2022-5-3 每日一题-leetcode

作者:互联网

题目链接:https://leetcode-cn.com/problems/reorder-data-in-log-files/

个人题解:

  1. 根据题意:数字日志 应该保留原来的相对顺序。因此要采用 \(stableSort()\) 函数
  2. 根据题目意思模拟即可
点击查看代码
class Solution {
public:
    vector<string> reorderLogFiles(vector<string>& logs) {
        stable_sort(logs.begin(), logs.end(), [](const string& a, const string& b) {
            int x = a.find(' '), y = b.find(' ');
            auto a1 = a.substr(0, x), b1 = b.substr(0, y);
            auto a2 = a.substr(x + 1), b2 = b.substr(y + 1);
            auto a3 = isdigit(a2[0]), b3 = isdigit(b2[0]);
            if (a3 != b3) return a3 < b3;
            if (a3) return false;
            if (a2 != b2) return a2 < b2;
            return a1 < b1;
        });
        return logs;
    }
};

image

标签:return,b2,substr,a3,a2,2022,一题,leetcode,logs
来源: https://www.cnblogs.com/cytcnblogs/p/16217521.html