[leetcode] 36 有效数独

leetcode 36

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.


image.png

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
Example 1:
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2:
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
The given board size is always 9x9.

有效数独

题目的意思就是 保证
每行
每列
每3x3 的小方格 内 不允许出现重复数字
于是乎 ,我就依次检查
每行
每列
每3x3
..............
这题应该有啥优雅的写法,我先写个不用动脑的。。

#include <unordered_map>

using namespace std;
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        //row
        for(int i = 0; i < 9; i++){
            up.clear();
            for(int j = 0; j < 9;j++){
                up[board[i][j]]++;
                if( board[i][j] !='.' && up[board[i][j]] > 1 )
                    return false;
            }
        }
        
        // column
        for(int i = 0; i < 9; i++){
            up.clear();
            for(int j = 0; j < 9;j++){
                up[board[j][i]]++;
                if( board[j][i] !='.' && up[board[j][i]] > 1 )
                    return false;
            }
        }
        // 3X3
        for(int i = 0; i < 9 ; i+=3){
            for(int j = 0; j < 9; j+=3){
                if (!isValid33(board, i,j))
                    return false; 
            }
        }
        
        return true;
        
        
    }
    
private:
    unordered_map<char,int> up;
    bool isValid33(vector<vector<char>>& board,int x, int y){
        up.clear();
        for(int i = x; i < x+3; i++){
            for(int j = y; j < y +3; j++){
                up[board[i][j]]++;
                if( board[i][j] !='.' && up[board[i][j]] > 1 )
                    return false;
            }
        }
        return true;
    }
};
image.png

所以这题 就是考 行, 列,3X3 在一个循环内的对应关系。
行转列很简单
主要是 行 转 3 X3
比如

(0,0), (0,1),(0,2) | (0,3),(0,4),(0,5)| (0,6),(0,7),(0,8) |
(1,0), (1,1),(0,2) | (1,3),(1,4),(1,5)| (1,6),(1,7),(1,8) |
.....
(3,0), (3,1),(3,2) | (3,3),(3,4),(3,5)| (3,6),(3,7),(3,8) |

我们希望在遍历行的同时,把 第一个3X3 映射到第0 个哈希表中, 第二个3x3映射到第二个哈希表中。。。以此类推
我们发现 i 增的比较慢(外循环)
i/3 的序列是

0,0,0,0,0,0,0,0,0,
......x 2
1,1,1,1,1,1,1,1,1,
......x 2
2,2,2,2,2,2,2,2,2
.......x 2

j增的比较快(内循环)
j/3 的序列是

0,0,0,1,1,1,2,2,2
0,0,0,1,1,1,2,2,2
0,0,0,1,1,1,2,2,2
.....x 6

我们想要的 序列是:

 0,0,0,1,1,1,2,2,2
...x 2
3,3,3,4,4,4,5,5,5
...x 2
6,6,6,7,7,7,8,8,8
..x 2

所以我们不难得出, i/3*3 + j/3 就是最终的 3x3 对应的哈希表编号

搞定 循环内 row,col, 3x3 的对应哈希表下标, 就可以把3个循环写在一起了。
这里因为只有9个数字(1..9), 所以我使用 比特位来做哈希数组,节省空间.

比特位作哈希数组,就是运用3个操作(<< , & ,|)
1<< k , 把 1 向左移动K位,
类似 hash_map[5] ==1
使用比特位就是 (00000001) << 5 == (00010000)
这样第5 个比特位就占上了,
如果再出现5 , 那么(00010000) & (00010000) 就会不等于0 。
反之, 就把该数字 | 到比特数组上, 把该比特位占上。

代码如下。

#include <vector>
using namespace std;
class Solution {
private:
   vector<int> row = vector<int>(9);
   vector<int> col =vector<int>(9);
   vector<int> sub  = vector<int>(9);
   int sub_index = 0;
   int bits;
public:
   bool isValidSudoku(vector<vector<char>>& board) {
       for(int i = 0;i < 9; i++){
           for(int j = 0; j < 9; j++){
               if(board[i][j] != '.'){
                   sub_index = i/3*3+ j/3;
                   // 使用 1个数字的不同比特位, 来当作一个哈希数组
                   bits = (1<< (board[i][j]-'0'));
                   if(row[i] & bits || col[j] & bits || sub[sub_index] & bits)
                       return false;// 有比特位被占, 说明有数字已经出现过
                       
                   // 没有出现过, 把该比特位占上
                   row[i] |=bits;
                   col[j] |=bits;
                   sub[sub_index] |=bits; 
               }     
           }
   } 
       return true;
   }
};
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,922评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,591评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,546评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,467评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,553评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,580评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,588评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,334评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,780评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,092评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,270评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,925评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,573评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,194评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,437评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,154评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,127评论 2 352

推荐阅读更多精彩内容