Why my code is giving the wrong answer?


  • 0
    S

    class Solution {
    public:
    void solve(vector<vector<char>>& board) {
    if(board.size()==0)
    return;
    int n=board[0].size();
    int m=board.size()/board[0].size();
    int i=1,j=1;
    for(i=1;i<m-1;i++)
    {
    for(j=1;j<n-1;j++)
    {
    if(board[i][j]=='O')
    {
    if(down(board,i,j,m) && (up(board,i,j) && (left(board,i,j) && right(board,i,j,n))))
    board[i][j]='X';
    }
    }
    }
    }
    int up(vector<vector<char>>& board,int i,int j)
    {
    while(i>0)
    {
    if(board[i-1][j]=='X')
    return 1;
    i--;
    }
    return 0;

    }
    int down(vector<vector<char>>& board,int i,int j,int n)
    {
        while(i<n-1)
        {
            if(board[i+1][j]=='X')
            return 1;
            i++;
        }
        return 0;
        
        
    }
    
    int left(vector<vector<char>>& board,int i,int j)
    {
        while(j>0)
        {
            if(board[i][j-1]=='X')
            return 1;
            j--;
        }
        return 0;
        
        
    }
    int right(vector<vector<char>>& board,int i,int j,int n)
    {
        while(j<n-1)
        {
            if(board[i][j+1]=='X')
            return 1;
            j++;
        }
        return 0;
        
        
    }
    

    };


Log in to reply
 

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