【Leetcode】36. Valid Sudoku

来源:互联网 发布:双色球算法公式技巧 编辑:程序博客网 时间:2024/06/05 18:11

36. Valid Sudoku

  • Total Accepted: 85441
  • Total Submissions: 266223
  • Difficulty: Easy

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.


注意:

1、map.clear()

2、判断flag是否为false每次做完都判断。

代码:

public class Solution {    public boolean isValidSudoku(char[][] board) {        HashMap<Character,Boolean> map = new HashMap<Character,Boolean>();        boolean flag = true;        for(int i = 0 ; i < 9 ; i++){            flag = checkCol(i,board,map);            if(!flag) return flag;//每次都判断            flag = checkRow(i,board,map);            if(!flag) return flag;        }        for(int i = 0 ; i < 9 ; i+=3)            for(int j = 0 ; j < 9 ; j+=3){                flag = checkBlock(i,j,board,map);                if(!flag) return flag;            }            return flag;    }    private boolean checkCol(int j,char[][] board,HashMap<Character,Boolean> map){        map.clear();        for(int k = 0 ; k < 9 ; k++){            if(board[k][j] != '.'){                if(map.containsKey(board[k][j])){                   return false;                }                 map.put(board[k][j],true);            }        }        return true;    }    private boolean checkRow(int i,char[][] board,HashMap<Character,Boolean> map){        map.clear();        for(int k = 0 ; k < 9 ; k++){            if(board[i][k] != '.'){                if(map.containsKey(board[i][k])) return false;                map.put(board[i][k],true);            }        }        return true;        }    private boolean checkBlock(int row,int col,char[][] board,HashMap<Character,Boolean> map){        map.clear();        for(int i = row ; i < row+3 ; i++){            for(int j = col ; j < col+3 ; j ++){            if(board[i][j] != '.'){                if(map.containsKey(board[i][j])) return false;                map.put(board[i][j],true);            }            }        }        return true;    }}

代码2:http://www.cnblogs.com/ganganloveu/p/4170632.html

class Solution {public:    bool isValidSudoku(vector<vector<char> > &board) {        for(int i = 0; i < 9; i ++)        {            unordered_map<char, bool> m1;   //check i_th row            unordered_map<char, bool> m2;   //check i_th column            unordered_map<char, bool> m3;   //check i_th subgrid            for(int j = 0; j < 9; j ++)            {                if(board[i][j] != '.')                {                    if(m1[board[i][j]] == true)                        return false;                    m1[board[i][j]] = true;                }                if(board[j][i] != '.')                {                    if(m2[board[j][i]] == true)                        return false;                    m2[board[j][i]] = true;                }                if(board[i/3*3+j/3][i%3*3+j%3] != '.')                {                    if(m3[board[i/3*3+j/3][i%3*3+j%3]] == true)                        return false;                    m3[board[i/3*3+j/3][i%3*3+j%3]] = true;                }            }        }        return true;    }};


0 0
原创粉丝点击