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

[LeetCode] Valid Sudoku

时间:2015-09-08 18:35:38      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

This problem is relatively easy. The challenge is how to get a concise code. This post shares a very elegant one which traverses each cell of board only once. The code is rewritten below.

 1 class Solution {
 2 public:
 3     bool isValidSudoku(vector<vector<char>>& board) {
 4         bool row[9][9] = {false}, col[9][9] = {false}, box[9][9] = {false};
 5         for (int i = 0; i < 9; i++) {
 6             for (int j = 0; j < 9; j++) {
 7                 if (board[i][j] != .) {
 8                     int num = board[i][j] - 0 - 1, k = i / 3 * 3 + j / 3;
 9                     if (row[i][num] || col[j][num] || box[k][num]) return false;
10                     row[i][num] = col[j][num] = box[k][num] = true;
11                 }
12             }
13         }
14         return true;
15     }
16 };

 

[LeetCode] Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4792475.html

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