【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
- [LeetCode]36.Valid Sudoku
- LeetCode --- 36. Valid Sudoku
- LeetCode 36.Valid Sudoku
- [Leetcode] 36. Valid Sudoku
- 【leetcode】36. Valid Sudoku
- [leetcode] 36. Valid Sudoku
- Leetcode 36. Valid Sudoku
- 36. Valid Sudoku LeetCode
- leetcode 36. Valid Sudoku
- LeetCode *** 36. Valid Sudoku
- leetcode 36. Valid Sudoku
- [LeetCode]36. Valid Sudoku
- LeetCode 36. Valid Sudoku
- leetcode-36. Valid Sudoku
- 36.[Leetcode]Valid Sudoku
- Leetcode 36.Valid Sudoku
- LeetCode-36.Valid Sudoku
- leetcode 36. Valid Sudoku
- linux内核 路由缓存表之创建
- csu 1105 NBUT1108 打怪升级
- JS横向循环滑动也可移动性切换
- 开源项目circular-progress-button源码分析
- Intent四个重要属性
- 【Leetcode】36. Valid Sudoku
- BeautifulSoup 提取某个tag标签里面的内容
- RTSP流媒体花屏
- 《thinking In UML》读书笔记(四)模型的关系表示
- QT中QTableWidget的使用
- uva10305-拓扑排序
- ArrayList 集合的应用
- QTableWidget 点击标题栏 设置排序
- linux内核 策略路由之基本结构