码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Valid Sudoku

时间:2015-03-01 15:45:20      阅读:136      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!