my 7ms solution for c++


  • 0
    L

    '''
    class Solution
    {
    public:
    bool backTrack(vector<vector<char>>&board,vector<pair<int,int>>&hole,int deep,int col[],int row[],int block[])
    {
    if(hole.size()==deep)return true;
    int c=hole[deep].first;
    int r=hole[deep].second;
    int blk=c/33+r/3;
    for(int val=1;val<=9;val++)
    {
    int bit=1<<val;
    if(col[c]&bit||row[r]&bit||block[blk]&bit)
    continue;
    col[c] |= bit;
    row[r] |= bit;
    block[blk] |= bit;
    board[c][r]=val+'0';
    if(backTrack(board,hole,deep+1,col,row,block))
    return true;
    col[c] &= ~bit;
    row[r] &= ~bit;
    block[blk] &= ~bit;
    board[c][r]='.';
    }
    return false;
    }
    void solveSudoku(vector<vector<char>>& board)
    {
    vector<pair<int,int>>hole;
    int col[9]={0};
    int row[9]={0};
    int block[9]={0};
    /标记所有'.'和数据/
    for(int c=0;c<9;c++)
    {
    for(int r=0;r<9;r++)
    {
    if(board[c][r]=='.')
    {
    hole.emplace_back(c,r);
    continue;
    }
    int val=board[c][r]-'0';
    col[c] |= 1<<val;
    row[r] |= 1<<val;
    block[c/3
    3+r/3] |= 1<<val;
    }
    }
    backTrack(board,hole,0,col,row,block);
    }
    };
    '''


  • 0
    Q

    I copied your code but got an wrong answer:
    Input:
    ["..9748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.."]
    Output:
    ["..9748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.."]
    Expected:
    ["519748632","783652419","426139875","357986241","264317598","198524367","975863124","832491756","641275983"]


Log in to reply
 

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