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

36 Valid Sudoku

时间:2015-07-08 19:10:38      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:leetcode

36 Valid Sudoku

链接:https://leetcode.com/problems/valid-sudoku/
问题描述:
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.

Hide Tags Hash Table
验证数独是会否有效,即每一行,每一列,每个小方块内1-9这9个数字只能出现一次。其实判断起来很简单。但是需要搞清楚输入数据是怎么样的。输入数据是

vector<vector<char>>& board

board[i][j]表示第i行,j列的数据。

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) 
    {
       char table1[9][9],table2[9][9],table3[9][9];
       memset(table1,0,81*sizeof(char));
       memset(table2,0,81*sizeof(char));
       memset(table3,0,81*sizeof(char));

        for(int i=0;i<9;i++)
        {
          for(int j=0;j<9;j++)
          {
              if(board[i][j]>=‘1‘&&board[i][j]<=‘9‘)
              {
               if( table1[i/3*3+j/3][board[i][j]-‘1‘]==1 ||
                   table2[i][board[i][j]-‘1‘]==1 ||
                   table3[j][board[i][j]-‘1‘]==1)
                   return false;
               table1[i/3*3+j/3][board[i][j]-‘1‘]=1;
               table2[i][board[i][j]-‘1‘]=1;
               table3[j][board[i][j]-‘1‘]=1;
              }
          }
        }
        return true;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

36 Valid Sudoku

标签:leetcode

原文地址:http://blog.csdn.net/efergrehbtrj/article/details/46804961

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