-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path937-reorderLogFiles.h
More file actions
54 lines (47 loc) · 1.2 KB
/
937-reorderLogFiles.h
File metadata and controls
54 lines (47 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <vector>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
/*
给你一个日志数组 logs。每条日志都是以空格分隔的字串,其第一个字为字母与数字混合的 标识符 。
有两种不同类型的日志:
字母日志:除标识符之外,所有字均由小写字母组成
数字日志:除标识符之外,所有字均由数字组成
请按下述规则将日志重新排序:
所有 字母日志 都排在 数字日志 之前。
字母日志 在内容不同时,忽略标识符后,按内容字母顺序排序;在内容相同时,按标识符排序。
数字日志 应该保留原来的相对顺序。
返回日志的最终顺序。
*/
using namespace std;
class Solution937 {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
//
stable_sort(logs.begin(), logs.end(), [](const string& log1, const string& log2) {
int pos1 = log1.find_first_of(" ");
int pos2 = log2.find_first_of(" ");
bool isDigit1 = isdigit(log1[pos1 + 1]);
bool isDigit2 = isdigit(log2[pos2 + 1]);
if (isDigit1 && isDigit2) {
return false;
}
//
if (!isDigit1 && !isDigit2) {
string s1 = log1.substr(pos1);
string s2 = log2.substr(pos2);
if (s1 != s2) {
return s1 < s2;
}
//
return log1 < log2;
}
return isDigit1 ? false : true;
});
//
return logs;
}
};