- 로그를 재정렬하라. 기준은 다음과 같다.
- 로그의 가장 앞 부분은 식별자다.
- 문자로 구성된 로그가 숫자 로그보다 앞에 온다.
- 식별자는 순서에 영향을 끼치지 않지만, 문자가 동일한 경우 식별자 순으로 한다.
- 숫자 로그는 입력 순서대로 한다.
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".
Input: logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> result;
int n = 0;
for (int i = 0; i < logs.size(); i++)
{
n = logs[i].find(" ");
if (isalpha(logs[i][n + 1]) != 0) result.push_back(logs[i]);
}
sort(result.begin(), result.end(), [](string& a, string& b)
{
int as = a.find(" ");
int bs = b.find(" ");
if (a.substr(as) == b.substr(bs)) return a.substr(0, as) < b.substr(0, as);
return a.substr(as) < b.substr(bs);
});
for (int i = 0; i < logs.size(); i++)
{
n = logs[i].find(" ");
if (isdigit(logs[i][n + 1]) != 0) result.push_back(logs[i]);
}
return result;
}
};
for (int i = 0; i < logs.size(); i++)
{
//1.
n = logs[i].find(" ");
//2.
if (isalpha(logs[i][n + 1]) != 0) result.push_back(logs[i]);
}
//1.
sort(result.begin(), result.end(), [](string& a, string& b)
{
//2.
int as = a.find(" ");
int bs = b.find(" ");
//3.
if (a.substr(as) == b.substr(bs)) return a.substr(0, as) < b.substr(0, as);
//4.
return a.substr(as) < b.substr(bs);
});
for (int i = 0; i < logs.size(); i++)
{
//1.
n = logs[i].find(" ");
//2.
if (isdigit(logs[i][n + 1]) != 0) result.push_back(logs[i]);
}
Runtime 11 ms / Memory 13.3 MB
https://leetcode.com/problems/reorder-data-in-log-files/submissions/879947683/