首页 > 其他 > 详细

leetcode 36. Valid Sudoku

时间:2019-09-15 14:40:40      阅读:98      评论:0      收藏:0      [点我收藏+]

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

技术分享图片
A partially filled sudoku which is valid.

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的子格子数字不能重复。 

 1 class Solution {
 2 public:
 3     bool isValidSudoku(vector<vector<char>>& board) {
 4         //map<int, map<char, int> > row, col, grid;
 5         vector<vector<int> > row(9, vector<int>(10, 0)), col(9, vector<int>(10, 0)), grid(9, vector<int>(10, 0));
 6         for (int i = 0; i < 9; i++) {
 7             for (int j = 0; j < 9; j++) {
 8                 char tmp = board[i][j];
 9                 if (tmp != .) {
10                     int num = tmp - 0;
11                     if (row[i][num] != 0) {
12                         return false;
13                     } else {
14                         row[i][num] = 1;
15                     }
16                     
17                     if (col[j][num] != 0) {
18                         return false;
19                     } else {
20                         col[j][num] = 1;
21                     }
22                     // 从左往右,从上至下,3x3格子分别从0-8编号。
23                     if (grid[(i / 3) * 3 + (j / 3)][num] != 0) {
24                         return false;
25                     } else {
26                         grid[(i / 3) * 3 + (j / 3)][num] = 1;
27                     }
28                 }
29                 
30             }
31         }
32         return true;
33     }
34 };

 

leetcode 36. Valid Sudoku

原文:https://www.cnblogs.com/qinduanyinghua/p/11521838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!