15 line C++ solution


  • 0
    N
    class Solution {
    public:
        vector<int> findDiagonalOrder(vector<vector<int>>& matrix) {
            vector<int> ret;
            if (matrix.size() == 0 || matrix[0].size() == 0) return ret;
            for (int k = 0; k <= (int)(matrix.size() + matrix[0].size()) - 2; ++ k){
                for (int i = 0; i <= k; ++ i){
                    int x = i, y = k - i;
                    if (k % 2 == 0) swap(x, y);
                    if (x >= 0 && x < matrix.size() && y >= 0 && y < matrix[0].size()) ret.push_back(matrix[x][y]);
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.