LeetCode2.1.14 @ Valid Sudoku 验证数独D2F2
来源:互联网 发布:淘宝数据包 编辑:程序博客网 时间:2024/06/02 06:21
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 '.'
.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
There are just 3 rules to Sudoku.
Each row must have the numbers 1-9 occuring just once.
Each column must have the numbers 1-9 occuring just once.
And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
用HashMap记录2D-Array的值,再查重
public class Solution { public boolean isValidSudoku(char[][] board) { for (int i = 0; i < 9; i++) { HashMap<Character, Character> hm = new HashMap<Character, Character>(); for (int j = 0; j < 9; j++) { char c = board[i][j];//【注意1】second[] change more freq,so it's checking rowSet if (c=='.') { } else{ //【注意2】 if (hm.get(c)==null) { hm.put(c, c); }else { return false; } } } } for (int i = 0; i < 9; ++i) { HashMap<Character, Character> hm = new HashMap<Character, Character>(); for (int j = 0; j < 9; ++j) { char c = board[j][i];//【注意1】first[] change more freq,so it's checking colSet if (c=='.') { } else{ //【注意2】 if (hm.get(c)==null) { hm.put(c, c); }else { return false; } } } } return isSmallValid(board, 0, 0) && isSmallValid(board, 0, 3) && isSmallValid(board, 0, 6) && isSmallValid(board, 3, 0) && isSmallValid(board, 3, 3) && isSmallValid(board, 3, 6) && isSmallValid(board, 6, 0) && isSmallValid(board, 6, 3) && isSmallValid(board, 6, 6); } private boolean isSmallValid(char[][] board, int x, int y) { //【注意3】x,y:start-row-index,start-col-index of the subbox,they are included. HashMap<Character, Character> hm = new HashMap<Character, Character>(); for (int i = x; i < 3 + x; i++) { for (int j = y; j < 3 + y; j++) { char c = board[i][j]; if (c=='.') { } else{ if (hm.get(c)==null) { hm.put(c, c); }else { return false; } } } } return true; } }
【注意1】无论check行集合,还是列集合,都是外圈i,里圈j(别用row和col做变量,否则易错)。
关键在于board[i][j],还是board[j][i]。
【注意2】if--else的逻辑 VS if--else if--else 完全不一样!在这里出错了。
【注意3】isSmallValid的参数是char[][]。 因为写成char[]编译错误。
【注意4】每个for,if,else请配上括号!因为这个问题使结果错误。
附:第一次写的错误code
public class Solution { public boolean isValidSudoku(char[][] board) { //check for subbox HashSet<Character> subboxSet=new HashSet<Character>(); for(int i=0;i<3*3;i+=3) for(int j=0;j<3*3;j+=3) for(int r=0;r<3;r++) for(int c=0;c<3;c++){ if(board[i+r][j+c]=='.'){ //no matter what the loop invariant, 2D-Array is always [row][col] //do nothing } else if(subboxSet.contains(board[i+r][j+c])) return false; else subboxSet.add(board[i+r][j+c]); }//end inner for //check for ColmnSet,out loop is col,skill HashSet<Character> colSet=new HashSet<Character>(); for(int c=0;c<9;c++) for(int r=0;r<9;r++){ if(board[r][c]=='.'){ //do nothing } else if(colSet.contains(board[r][c])) return false; else colSet.add(board[r][c]); } //check for RowSet HashSet<Character> rowSet=new HashSet<Character>(); for(int c=0;c<9;c++) for(int r=0;r<9;r++){ if(board[r][c]=='.'){ //do nothing } else if(rowSet.contains(board[r][c])) return false; else rowSet.add(board[r][c]); } return true; }}
0 0
- LeetCode2.1.14 @ Valid Sudoku 验证数独D2F2
- Valid Sudoku:数独验证
- LeetCode Valid Sudoku数独有效性验证
- Leetcode #36. Valid Sudoku 数独游戏验证 解题报告
- 数独判断 Valid Sudoku
- Valid Sudoku-数独判断
- 有效数独 Valid Sudoku
- [LeetCode] [数独问题] Valid Sudoku
- Valid Sudoku 有效的数独
- LintCode Valid Sudoku 判断数独是否合法
- Valid Sudoku 数独的有效性
- Valid Sudoku 数独的有效性
- (LeetCode)Valid Sudoku --- 合法数独
- 36. Valid Sudoku (判断数独)
- leetcode(36) - Valid Sudoku 数独
- leetcode 36. Valid Sudoku 数独有效
- 【LeetCode-面试算法经典-Java实现】【036-Valid Sudoku(验证数独棋盘)】
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
- <深入理解C指针>学习笔记和总结 第六章 指针和结构体 第二部分
- Ubuntu安装texlive(vanilla installation)
- WPF TextBox 控件获取热键并转为 win32 Keys
- c++11绑定器bind
- 更新OPatch中OCM报错
- LeetCode2.1.14 @ Valid Sudoku 验证数独D2F2
- Chapter6 SQL:Data Manipulation
- 有关ReprojectImageTo3D的点云长得奇怪的解决方案
- SQL SERVER基础
- 开机,连网,application, 启动Service 记录
- 5.1.12 Populating Next Right Pointers in Each Node II
- Chapter 1 Databases and Database Users
- ubuntu 12.04 配置samba
- c++11调用成员函数mem_fn和适合普通函数指针