首页 > 其他 > 详细

LeetCode Valid Sudoku

时间:2015-03-01 15:43:38      阅读:190      评论:0      收藏:0      [点我收藏+]

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could 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.

题意:判断是否满足数独的条件。

思路:判断同一行,列,格子时候满足不同的数,数组判重就行了

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        int rows[10][10], cols[10][10], block[10][10];
        memset(rows, 0, sizeof(rows));
        memset(cols, 0, sizeof(cols));
        memset(block, 0, sizeof(block));

        for (int i = 0; i < 9; i++) 
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.') continue;
                int cur = board[i][j] - '0';
                if (rows[i][cur] || cols[j][cur] || block[i/3*3+j/3][cur])
                    return false;
                rows[i][cur] = cols[j][cur] = block[i/3*3+j/3][cur] = 1;
            }

        return true;
    }
};



LeetCode Valid Sudoku

原文:http://blog.csdn.net/u011345136/article/details/44003021

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!