Why did I get a Runtime error at OJ? It works in my Xcode.


  • 0
    L
    class Solution {
    public:
        void solve(vector<vector<char>>& board) {
            if (board.size() == 0) return;
            if (board[0].size() == 0) return;
            for (int j = 0; j < board[0].size(); j++){
                if (board[0][j] == 'O') f(0, j, board);
            }
            for (int j = 0; j < board[board.size() - 1].size(); j++){
                if (board[board.size() - 1][j] == 'O') f(board.size() - 1, j, board);
            }
            for (int i = 0; i < board.size(); i++){
               if (board[i][0] == 'O') f(i, 0, board);
               if (board[i][board[i].size() - 1] == 'O') f(i, board[i].size() - 1, board);
            }
            
            for (int i = 0; i < board.size(); i++){
                for (int j = 0; j < board[i].size(); j++){
                    if (board[i][j] == '1') board[i][j] = 'O';
                    else board[i][j] = 'X';
                }
            }
        }
        void f(int r, int c, vector<vector<char>>& board){
            queue<pair<int, int>> q;
            q.push(make_pair(r, c));
            while (q.size() > 0){
                pair<int, int> p = q.front();
                q.pop();
                int r = p.first;
                int c = p.second;
                board[r][c] = '1';
                if (r - 1 >= 1 && board[r - 1][c] == 'O') q.push(make_pair(r - 1, c));
                if (r + 1 <= board.size() - 2 && board[r + 1][c] == 'O') q.push(make_pair(r + 1, c));
                if (c - 1 >= 1 && board[r][c - 1] == 'O') q.push(make_pair(r, c - 1));
                if (c + 1 <= board[r].size() - 2 && board[r][c + 1] == 'O') q.push(make_pair(r, c + 1));
            }
        }
        
    };
    

    Last executed input:
    ["O"]


Log in to reply
 

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