[Leetcode] Valid Sudoku

Valid Sudoku 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/valid-sudoku/description/


Description

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 '.'.

example

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.

Solution


class Solution {
public:
    bool isValidSudoku(vector<vector<char> >& board) {
        int row, col;
        for (row = 0; row < 9; row++) {
            vector<bool> bucket(9, false);
            for (col = 0; col < 9; col++) {
                if (board[row][col] != '.') {
                    if (!bucket[board[row][col] - '1'])
                        bucket[board[row][col] - '1'] = true;
                    else
                        return false;
                }
            }
        }

        for (col = 0; col < 9; col++) {
            vector<bool> bucket(9, false);
            for (row = 0; row < 9; row++) {
                if (board[row][col] != '.') {
                    if (!bucket[board[row][col] - '1'])
                        bucket[board[row][col] - '1'] = true;
                    else
                        return false;
                }
            }
        }

        int rstart, cstart;
        for (rstart = 0; rstart < 9; rstart += 3) {
            for (cstart = 0; cstart < 9; cstart += 3) {
                vector<bool> bucket(9, false);
                for (row = rstart; row < rstart + 3; row++) {
                    for (col = cstart; col < cstart + 3; col++) {
                        if (board[row][col] != '.') {
                            if (!bucket[board[row][col] - '1'])
                                bucket[board[row][col] - '1'] = true;
                            else
                                return false;
                        }
                    }
                }
            }
        }
        return true;
    }
};


解题描述

这道题题意非常简单,就是求给出的半满九宫格是不是可以完成的。直接使用了暴力查找解决。

转载于:https://www.cnblogs.com/yanhewu/p/8340668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值