You have an array of logs.  Each log is a space delimited string of words.

For each log, the first word in each log is an alphanumeric identifier.  Then, either:

Each word after the identifier will consist only of lowercase letters, or;
Each word after the identifier will consist only of digits.
We will call these two varieties of logs letter-logs and digit-logs.  It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log.  The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties.  The digit-logs should be put in their original order.

Return the final order of the logs.



Example 1:

Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]


Constraints:

0 <= logs.length <= 100
3 <= logs[i].length <= 100
logs[i] is guaranteed to have an identifier, and a word after the identifier.

这题首先定义了两种日志格式,一种是全字符类型的日志,还有一种是全数字的类型。我们要对这种日志做个排序,需要将全字符日志的排在最前面,然后根据字典序做排序,然后把全数字的放在后面,不做排序。

题意中只说了全字符的排序,去掉第一个标识字符,然后做排序,所以我这里踩了个坑,没有对全字符内容一样的,再对标识符做排序,导致有一个case错了,后面改了之后就好了,这个要留心。

class ReorderDataInLogFiles : public Solution {
    public:
    void Execute() {
        std::cout << "Case 1: ";
        auto strs = reorderLogFiles(vector<string>{"dig1 8 1 5 1",
        "let1 art can",
        "dig2 3 6",
        "let2 own kit dig",
        "let3 art zero"});
        for (auto &v : strs) {
            std::cout << v << std::endl;
        }
        std::cout << std::endl;

        std::cout << "Case 2: ";
        strs = reorderLogFiles(vector<string>{"a1 9 2 3 1",
        "g1 act car",
        "zo4 4 7",
        "ab1 off key dog",
        "a8 act zoo",
        "a2 act car"});
        for (auto &v : strs) {
            std::cout << v << std::endl;
        }
        std::cout << std::endl;
    }

    vector<string> reorderLogFiles(const vector<string>& logs) {
        vector<string> out;
        vector<string> num_list;

        for (auto &v : logs) {
            bool blank = false;
            for (auto c : v) {
                if (c == ' ') {
                    blank = true;
                    continue;
                }
                if (blank) {
                    if (c >= '0' && c <= '9') {
                        num_list.push_back(std::move(v));
                    } else {
                        out.push_back(std::move(v));
                    }
                    break;
                }
            }
        }

        // Sort out
        std::sort(out.begin(), out.end(), [](const std::string& l, const std::string &r) -> bool {
            std::size_t i = l.find(' ');
            std::size_t j = r.find(' ');

            int result = strcmp(l.c_str() + i + 1, r.c_str() + j + 1);
            if (result != 0) {
                return result < 0;
            }
            return strcmp(l.c_str(), r.c_str()) < 0;
        });

        // Push number list
        for (auto &num : num_list) {
            out.push_back(std::move(num));
        }

        return out;
    }
};
共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day