c++ solution


  • 0
    vector<int> findDiagonalOrder(vector<vector<int>>& matrix) 
    {
        vector<int> result;
        if (!matrix.size() || !matrix[0].size())
            return result;
        int m = matrix.size();
        int n = matrix[0].size();
    
        for (int k = 0; k < m + n - 1; k++)
        {
            if (k & 1)
            {
                for (int i = max(0, k - n + 1); i <= min(k, m - 1); i++)
                    result.push_back(matrix[i][k - i]);
            }
            else
            {
                for (int i = min(k, m - 1); i >= max(0, k - n + 1); i--)
                    result.push_back(matrix[i][k - i]);
            }
        }
        
        return result;
    

    clear thart for each matrix[i][j], we get i + j = k


Log in to reply
 

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