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

[leetcode 36] valid sudoku

时间:2015-09-27 20:05:02      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

1 题目

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.

2 思路

开始就以为是完全填完,看最后能不能填出来。

结果题目意思是根据已有的数据,判断是否已经挂了。这也是看完别人的代码才知道的。。

 

3 代码:

 

        // only need to examin the providing num is Valid
        int len = board.length;
        Boolean[][] row = new Boolean[len][len];
        Boolean[][] column = new Boolean[len][len];
        Boolean[][] sub = new Boolean[len][len];
        
        for (int i = 0; i < sub.length; i++) {
            Arrays.fill(row[i], false);
            Arrays.fill(column[i], false);
            Arrays.fill(sub[i], false);
        }

        
        for(int i=0;i<len;i++){
            for(int j=0;j<len;j++){
                if(board[i][j] != .){
                    int num = board[i][j] - 1;
                    int k = i/3*3+j/3;
                    if(row[i][num] || column[j][num] || sub[k][num]){
                        return false;
                    }
                    row[i][num]=column[j][num]=sub[k][num] = true;
                }
            }
        }
        return true;
    }

 

[leetcode 36] valid sudoku

标签:

原文地址:http://www.cnblogs.com/lingtingvfengsheng/p/4842639.html

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