leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0937.cpp (958B)


      1 class Solution {
      2   public:
      3     vector<string> reorderLogFiles(const vector<string> &logs) const {
      4         const int n = size(logs);
      5 
      6         typedef tuple<bool, int, string, string> record;
      7         vector<record> vec(n);
      8 
      9         for (int i = 0; i < n; i++) {
     10             bool letter = false, space = false;
     11             int idx = 0;
     12             for (int j = 0; j < size(logs[i]); j++) {
     13                 if (logs[i][j] == ' ')
     14                     space = true;
     15                 else if (!space)
     16                     idx = j;
     17                 else {
     18                     letter = isalpha(logs[i][j]);
     19                     break;
     20                 }
     21             }
     22             vec[i] = {!letter, letter ? 0 : i, logs[i].substr(idx + 2), logs[i].substr(0, idx + 1)};
     23         }
     24 
     25         sort(begin(vec), end(vec));
     26         vector<string> res(n);
     27         for (int i = 0; i < n; i++)
     28             res[i] = get<3>(vec[i]) + " " + get<2>(vec[i]);
     29         return res;
     30     }
     31 };