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

[LeetCode] 36. Valid Sudoku

时间:2017-04-14 11:41:28      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:nbsp   character   question   term   check   date   tco   sudoku   box   

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.

 1 // From Discuss
 2 class Solution {
 3 public:
 4     bool isValidSudoku(vector<vector<char>>& board) {
 5         //used1: check each row
 6         //used2: check each column
 7         //used3: check each sub-boxes
 8         bool used1[9][9]={false}, used2[9][9]={false}, used3[9][9]={false};
 9         
10         for (int i = 0; i < board.size(); ++i){
11             for (int j = 0; j < board[i].size(); ++j){
12                 if (board[i][j] != .){
13                     int num = board[i][j] - 0 - 1;
14                     int k = i / 3 * 3 + j / 3;
15                     if (used1[i][num] || used2[j][num] || used3[k][num]){
16                         return false;
17                     }else{
18                         used1[i][num] = used2[j][num] = used3[k][num] = true;
19                     }
20                 }
21             }
22         }
23         
24         return true;
25     }
26 };

 

 

 

 

 
 

[LeetCode] 36. Valid Sudoku

标签:nbsp   character   question   term   check   date   tco   sudoku   box   

原文地址:http://www.cnblogs.com/amadis/p/6708019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!