Valid Sudoku

题目名称
Valid Sudoku—LeetCode链接

描述
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.

分析
There are just 3 rules to Sudoku.
1. Each row must have the numbers 1-9 occuring just once.
这里写图片描述
2. Each column must have the numbers 1-9 occuring just once.
这里写图片描述
3. And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
这里写图片描述
  

C++代码

class Solution {
public:
   bool isValidSudoku(vector<vector<char>>& board) {
    vector<short> col(9, 0);
    vector<short> block(9, 0);
    vector<short> row(9, 0);
    for (int i = 0; i < 9; i++)
     for (int j = 0; j < 9; j++) {
         if (board[i][j] != '.') {
             short idx = 1 << (board[i][j] - '1');
             if (row[i] & idx || col[j] & idx || block[i/3 * 3 + j / 3] & idx)
                return false;
            row[i] |= idx;
            col[j] |= idx;
            block[i/3 * 3 + j/3] |= idx;
         }
     }
     return true;
  }
};

总结
  代码是别人写的,这道题我自己的方法太复杂,用了别人的一个我感觉非常好的方法,参考这里c++ very simple and easy understand. using bit operation. 用的是位操作,这也是我在编程代码中第一次看到对位的操作,所以及时翻开C++ Primer补了一下。
  将数独的9*9格先按照行分成9行,存储到row中,row[0]~row[9]分别代表了第一行到第九行的值。
  row[0]表示第一行,为short类型,32位机器中占两个字节,16位,初始值为0,二进制表示为:00000000 00000000。在遍历过程中,用题目描述中给定的数独举例:
1. 第一个遇到的字符为’5’,idx=1 << ‘5’-‘1’,将16位的整型1左移4位,得到16,二进制表示为:00000000 00010000;
2. 此时,判断一下row[0] & ldx,结果为0,表明在row之前没有遇到数字5;
3. 然后更新row:row[0] |= idx。
如下图所示:
这里写图片描述

  这样,只需要遍历一次,就能够判断数独是否有效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值