-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create leetcode937_my_version_20220303.cpp
- Loading branch information
1 parent
8f2cfe7
commit 275b441
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class Solution | ||
{ | ||
public: | ||
vector<string> reorderLogFiles(vector<string> &logs) | ||
{ | ||
vector<string> dig; | ||
vector<pair<string, string>> let; | ||
vector<string> ans(logs.size()); | ||
|
||
for (string &log : logs) | ||
{ | ||
if (isDigitLog(log)) | ||
dig.push_back(log); | ||
else | ||
{ | ||
int i = 0; | ||
for (; i < log.size(); i++) | ||
{ | ||
if (log[i] == ' ') | ||
break; | ||
} | ||
|
||
let.push_back({log.substr(0, i + 1), log.substr(i + 1, log.size() - i - 1)}); | ||
} | ||
} | ||
|
||
sort(begin(let), end(let), [](pair<string, string> &p1, pair<string, string> &p2) | ||
{ return (p1.second != p2.second) ? p1.second < p2.second : p1.first < p2.first; }); | ||
|
||
for (int i = 0; i < let.size(); i++) | ||
{ | ||
ans[i] = let[i].first + let[i].second; | ||
} | ||
|
||
for (int i = 0; i < dig.size(); i++) | ||
{ | ||
ans[i + let.size()] = dig[i]; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
private: | ||
bool isDigitLog(string &log) | ||
{ | ||
int i = 0; | ||
|
||
while (log[i] != ' ') | ||
i++; | ||
// ignore first space | ||
i++; | ||
|
||
return isdigit(log[i]); | ||
} | ||
}; | ||
|
||
// Runtime: 3 ms, faster than 99.81% of C++ online submissions for Reorder Data in Log Files. | ||
// Memory Usage: 10.6 MB, less than 92.69% of C++ online submissions for Reorder Data in Log Files. | ||
// time complexity: O(n) | ||
// space complexity: O(n) |