leetcode

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

0498.cpp (956B)


0 class Solution { 1 public: 2 bool valid(int i, int m, int j, int n) { return i >= 0 && i <= m && j >= 0 && j <= n; } 3 4 void quick_adjust(int &i, int &j, bool &up) { 5 if (up) 6 i++; 7 else 8 j++; 9 up = !up; 10 } 11 12 void move(int &i, int &j, bool &up) { 13 if (up) { 14 i--; 15 j++; 16 } else { 17 i++; 18 j--; 19 } 20 } 21 22 vector<int> findDiagonalOrder(vector<vector<int>> &mat) { 23 vector<int> res; 24 bool up = true; 25 int i = 0, j = 0; 26 int m = mat.size() - 1, n = mat[0].size() - 1; 27 28 while (true) { 29 res.push_back(mat[i][j]); 30 if (i == m && j == n) break; 31 32 move(i, j, up); 33 34 if (!valid(i, m, j, n)) { 35 quick_adjust(i, j, up); 36 while (!valid(i, m, j, n)) 37 move(i, j, up); 38 } 39 } 40 41 return res; 42 } 43 };