Why correct in Custom TestCase, but runtime error when I submit? It is an easy case [[1,2,3],[4,5,6],[7,8,9]]


  • 0
    A

    Why correct in Custom TestCase, but runtime error when I submit? It is an easy case
    [
    [1,2,3],
    [4,5,6],
    [7,8,9]
    ]

    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            if (matrix.size() == 0 || matrix[0].size() == 0) return vector<int>();
            
            int m = matrix.size();
            int n = matrix[0].size();
            vector<int> order(m * n);
            int ind = 0;
            int r1, r2, c1, c2;
            for (int i = 0; i <= min(m, n) / 2; i++) {
                r1 = i;
                r2 = m - 1 - i;
                c1 = i;
                c2 = n - 1 - i;
                // cout << "1st row" << endl;
                if (c2 >= c1) {
                    for (int j = c1; j <= c2; j++) {
                        order[ind++] = matrix[r1][j];
                    }
                }
                // for (auto x: order) {cout << x << " "; } cout << endl;
                
                // cout << "1st col" << endl;
                if (r2 > r1) {
                    for (int j = r1+1; j <= r2; j++) {
                        order[ind++] = matrix[j][c2];
                    }
                }
                // for (auto x: order) {cout << x << " "; } cout << endl;
                
                // cout << "2nd row" << endl;
                if (r2 > r1 && c2 > c1) {
                    for (int j = c2-1; j >= c1; j--) {
                        order[ind++] = matrix[r2][j];
                    }
                }
                // for (auto x: order) {cout << x << " "; } cout << endl;
                
                // cout << "2nd col" << endl;
                if (r2 > r1 + 1 && c2 > c1) {
                    for (int j = r2-1; j >= r1+1; j--) {
                        order[ind++] = matrix[j][c1];
                    }
                }
                // for (auto x: order) {cout << x << " "; } cout << endl;
                
                // cout << endl;
            }
            return order;
        }
        
    };
    

Log in to reply
 

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