76ms C++ solution


  • 0
    C
    public:
        vector<int> findDiagonalOrder(vector<vector<int>>& matrix) {
            vector<int> ret;
            int n=matrix.size();
            if(n==0) return ret;
            int m=matrix[0].size();
            if(m==0) return ret;
            int k=0, i=0, j=0;
            for(k=0; k<=n+m-2; k++){
                int jstart=max(0,n<m?k-(min(n,m)-1):k-(max(n,m)-1));
                int jend=min(k,n<m?max(n,m)-1:min(n,m)-1);
                for((k%2==0)?j=jstart:j=jend ; (k%2==0)?j<=jend:j>=jstart; (k%2==0)?j++:j--){
                    i=k-j;
                    ret.push_back(matrix[i][j]);
                }
            }
            return ret;
        }
    };```

Log in to reply
 

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