[Leetcode] valid Sudoku

Source: Internet
Author: User

Determine if a Sudoku is valid, according to: Sudoku puzzles-the rules.

The sudoku board cocould be partially filled, where empty cells are filled with the character‘.‘.

A partially filled Sudoku which is valid.

 

Note:
A valid Sudoku Board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

 

This question seems very complicated, but it is actually very brainless, because there is no need to judge whether it is a solvable Sudoku board. When this board is traversed and a position not empty is found, the rows, columns, and 3x3 are checked respectively. If a duplicate is displayed, the result is invalid. If the traversal is complete, the result is legal.

    bool isValidSudoku(vector<vector<char> > &board) {            unordered_set<int> column;    unordered_set<int> square;    unordered_set<int> row;         // check for colum    for (int i = 0; i < 9; i++) {        for (int j = 0; j < 9; j++) {            char item = board[j][i];            if (item != ‘.‘) {                int num = atoi(&item);                if (column.find(num) == column.end()){                    column.insert(atoi(&item));                }                else {                    return false;                }            }        }        column.clear();    }    // check for row    for (int i = 0; i < 9; i++) {        for (int j = 0; j < 9; j++) {            char item = board[i][j];            if (item != ‘.‘) {                int num = atoi(&item);                if (row.find(num) == row.end()) {                    row.insert(num);                }                else {                    return false;                }            }        }        row.clear();    }        // check for 3x3    for (int offsetY = 0; offsetY < 3; offsetY++) {        for (int offsetX = 0; offsetX < 3; offsetX++) {                        for (int i = 3*offsetY; i < 3*offsetY+3; i++) {                for (int j = 3*offsetX; j < 3*offsetX+3; j++) {                    char item = board[i][j];                    if (item != ‘.‘) {                        int num = atoi(&item);                        if (square.find(num) == square.end()) {                            square.insert(num);                        }                        else {                            return false;                        }                    }                }            }            square.clear();                    }    }        return true;    }

 

[Leetcode] valid Sudoku

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.