937. Reorder Data in Log Files

The description of problem

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

Letter-logs: All words (except the identifier) consist of lowercase English letters.
Digit-logs: All words (except the identifier) consist of digits.
Reorder these logs so that:

The letter-logs come before all digit-logs.
The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
The digit-logs maintain their relative ordering.
Return the final order of the logs.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-data-in-log-files

example

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"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-data-in-log-files

The intuition for this problem

  1. pick out the digit logs and character logs into different vectors
  2. sort out the character logs

The codes for this

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
    vector<string> reorderLogFiles(vector<string>& logs) {
        vector<string> digit_logs;
        vector<string> character_logs;
        for (auto log : logs) {
            if (is_digit_log(log)) {
                digit_logs.push_back(log);
            } else {
                character_logs.push_back(log);
            }
        }
        // sort out the character_logs
        sort(character_logs.begin(), character_logs.end(), [](string& a, string& b) {
            string a_key = a.substr(a.find(' ') + 1);
            string b_key = b.substr(b.find(' ') + 1);
            if (a_key == b_key) {
                return a < b;
            } else {
                return a_key < b_key;
            }
        });
        // merge the two vectors
        vector<string> result;
        result.reserve(logs.size());
        result.insert(result.end(), character_logs.begin(), character_logs.end());
        result.insert(result.end(), digit_logs.begin(), digit_logs.end());
        return result;
    }
    bool is_digit_log(string log) {
        // check if the char after the first space is a digit
        return log[log.find(' ') + 1] >= '0' && log[log.find(' ') + 1] <= '9';
    }
};

int main()
{
    vector<string> logs = {"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"};
    Solution _s;
    vector<string> result = _s.reorderLogFiles(logs);
    for (auto log : result) {
        cout << log << endl;
    }
}

The corresponding results

let1 art can
let3 art zero
let2 own kit dig
dig1 8 1 5 1
dig2 3 6

在这里插入图片描述

The little improvement for above codes

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
    vector<string> reorderLogFiles(vector<string>& logs) {
        vector<string> digits_logs;
        vector<string> letters_logs;
        for (auto log : logs) {
            if (isdigit(log[log.find(' ') + 1])) {
                digits_logs.push_back(log);
            } else {
                letters_logs.push_back(log);
            }
        }
        sort(letters_logs.begin(), letters_logs.end(), [](string& a, string& b) {
            string a_key = a.substr(a.find(' ') + 1);
            string b_key = b.substr(b.find(' ') + 1);
            if (a_key == b_key) {
                return a < b;
            } else {
                return a_key < b_key;
            }
        });
        letters_logs.insert(letters_logs.end(), digits_logs.begin(), digits_logs.end());
        return letters_logs;
    }
};

int main()
{
    vector<string> logs = {"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"};
    Solution _s;
    vector<string> result = _s.reorderLogFiles(logs);
    for (auto log : result) {
        cout << log << endl;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值