help! why got Runtime Error but the output is TRUE?


  • 0
    S

    my code is as follow, the main idea is dfs the four boundary and set 'O' flag to 'v', then change 'O' to 'X' and 'v' to 'O'.
    But I got the Runtime Error while the output is the same as the expected output?
    Any one can help me to point out where is the error?

    class Solution {
        void dfs(vector<vector<char>>& board, int i, int j) {
            int m = board.size(), n = board[0].size();
            if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] == 'X' || board[i][j] == 'v')
                return;
            board[i][j] = 'v';
            dfs(board, i + 1, j);
            dfs(board, i - 1, j);
            dfs(board, i, j + 1);
            dfs(board, i, j - 1);
        }
    public:
        void solve(vector<vector<char>>& board) {
            if (board.size() == 0 || board[0].size() == 0)
                return;
            int m = board.size(), n = board[0].size();
            for (int i = 0; i < m; i++) {
                dfs(board, i, 0);
                dfs(board, i, n-1);
            }
            for (int j = 0; j < n; j++) {
                dfs(board, 0, j);
                dfs(board, m-1, j);
            }
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (board[i][j] == 'O')
                        board[i][j] = 'X';
                    else if (board[i][j] == 'v')
                        board[i][j] = 'O';
                }
            }
        }
    };
    

Log in to reply
 

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