博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode——36. Valid Sudoku
阅读量:4137 次
发布时间:2019-05-25

本文共 898 字,大约阅读时间需要 2 分钟。

题目

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:

解答

判断一个数独是否有效,开始时理解错题意了,这里只管数独是否有效,而不需要关心填上的数字是否能解开这个数独。

所以就是判断每行,每列,每块不能有重复数字。O(N^2)。
其中第二个方法是bit操作,有意思!

class Solution {public:    bool isValidSudoku(vector
>& board) { bool num1[9][9]={
0},num2[9][9]={
0},num3[9][9]={
0}; for(int i=0;i
>& board) { vector
col(9, 0); vector
block(9, 0); vector
row(9, 0); for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) { if (board[i][j] != '.') { int idx = 1 << (board[i][j] - '0'); 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; }};

转载地址:http://oexvi.baihongyu.com/

你可能感兴趣的文章
阅读笔记《c++ primer》
查看>>
阅读笔记《C++标准程序库》
查看>>
基于mirror driver的windows屏幕录像
查看>>
C语言8
查看>>
Qt实现简单延时
查看>>
qml有关矩形说明
查看>>
在qt中使用QSplitter设置初始比例setStretchFactor失效的解决方法
查看>>
repeater的使用
查看>>
qt msvc编译中文乱码解决
查看>>
qt中TextField输入框无法输入中文解决办法
查看>>
qt实现点击出现窗口,点击其他任何地方窗口消失
查看>>
QML DropArea拖拉文件事件
查看>>
CORBA links
查看>>
读后感:&gt;
查看>>
ideas about sharing software
查看>>
different aspects for software
查看>>
To do list
查看>>
Study of Source code
查看>>
如何使用BBC英语学习频道
查看>>
spring事务探索
查看>>