[leetcode]Valid Sudoku

原创 2015年11月18日 17:09:47
class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        int row,col,block;
        char ch;
        vector<int> flags(9,0);

        //rows
        for(row=0;row<9;row++){
            flags.assign(9,0);
            for(col=0;col<9;col++){
                ch=board[row][col];
                if(ch!='.'){
                    if(flags[ch-'1']>0){
                        return false;
                    }else{
                        flags[ch-'1']++;
                    }
                }
            }
        }
        //cols
        for(col=0;col<9;col++){
            flags.assign(9,0);
            for(row=0;row<9;row++){
                ch=board[row][col];
                if(ch!='.'){
                    if(flags[ch-'1']>0){
                        return false;
                    }else{
                        flags[ch-'1']++;
                    }
                }
            }
        }
        //blocks
        for(block=0;block<9;block++){
            int srow=(block/3)*3;
            int scol=(block%3)*3;
            flags.assign(9,0);
            for(row=srow;row<srow+3;row++){
                for(col=scol;col<scol+3;col++){
                    ch=board[row][col];
                    if(ch!='.'){
                        if(flags[ch-'1']>0){
                            return false;
                        }else{
                            flags[ch-'1']++;
                        }
                    }
                }
            }
        }
        return true;
    }
};
class Solution {
public:
    bool checkValid(int vec[],int val){
        if(vec[val]==1){
            return false;
        }
        vec[val]=1;
        return true;
    }

    bool isValidSudoku(vector<vector<char>>& board) {
        int rowValid[10]={0};
        int colValid[9][10]={0};
        int subBoardValid[9][10]={0};

        for(int i=0;i<9;++i){
            memset(rowValid,0,sizeof(rowValid));
            for(int j=0;j<9;++j){
                if(board[i][j]!='.'){
                    if(!checkValid(rowValid,board[i][j]-'0')||!checkValid(colValid[j],board[i][j]-'0')||!checkValid(subBoardValid[i/3*3+j/3],board[i][j]-'0')){
                        return false;
                    }
                }
            }
        }
        return true;
    }
};

优化空间

class Solution {
public:
    bool checkValid(int vec[],int val){
        if(val<0){//字符'.'
            return true;
        }
        if(vec[val]==1){
            return false;//already exist
        }
        vec[val]=1;
        return true;
    }

    bool isValidSudoku(vector<vector<char>>& board) {
        int rowValid[10]={0};
        int colValid[10]={0};
        int subBoardValid[10]={0};
        for(int i=0;i<9;++i){
            memset(rowValid,0,sizeof(rowValid));
            memset(colValid,0,sizeof(colValid));
            memset(subBoardValid,0,sizeof(subBoardValid));
            for(int j=0;j<9;++j){
                //if(board[i][j]!='.'){
                    if(!checkValid(rowValid,board[i][j]-'0')||!checkValid(colValid,board[j][i]-'0')||!checkValid(subBoardValid,board[3*(i/3)+j/3][3*(i%3)+j%3]-'0')){
                        return false;
                    }
                //}
            }
        }
        return true;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-36】Valid Sudoku

这道题做起来非常简单了,开始错误的理解了题目的意思,首先判断每一行是否有重复元素,再判断每一列是否有重复元素,最后判断每一个9宫格是否有重复元素 判断9宫格时,要细心一些。 public clas...

Leetcode--Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be...
  • yskyj
  • yskyj
  • 2014-10-24 15:56
  • 221

[LeetCode OJ]ZigZag Conversion && Valid Sudoku && String to Integer (atoi)

ZigZag Conversion我的思路: 1、将一个锯齿状排列的字符串拉成一条线,输入到一个新的字符串中。 2、只要弄清楚循环的特征,算好nRows的变化给循环带来的变化,还是很容易完成。 ...

[LeetCode] 036. Valid Sudoku (Easy) (C++)

[LeetCode] 036. Valid Sudoku (Easy) (C++)
  • hcbbt
  • hcbbt
  • 2015-03-13 19:13
  • 1405

[leetcode]Valid Sudoku(判断有效数独 C语言实现)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku bo...

LeetCode | 36. Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be p...

leetcode 36. Valid Sudoku

解题思路: 直接判断,9行,9列,9个子面板是否符合要求原题目:Determine if a Sudoku is valid, according to: [Sudoku Puzzles - The...

LeetCode OJ算法题(三十五):Valid Sudoku

题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa...
  • op_yu
  • op_yu
  • 2014-07-25 11:48
  • 243

leetcode36~Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be...

[LeetCode] Valid Sudoku

Valid Sudoku   Total Accepted: 12421 Total Submissions: 44620
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)