leetcode做题总结,题目Valid Sudoku 2012/03/03
来源:互联网 发布:阿里云os recovery 编辑:程序博客网 时间:2024/06/05 03:41
数独游戏,横,竖,方框的9个数都不能相同,我的方法是用boolean数组即可。
public class Solution { public boolean isValidSudoku(char[][] board) { boolean[] b=new boolean[9]; for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ if(board[i][j]=='.') continue; if(b[Integer.parseInt(board[i][j]+"")-1]==false) b[Integer.parseInt(board[i][j]+"")-1]=true; else return false; } b=new boolean[9]; } for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ if(board[j][i]=='.') continue; if(b[Integer.parseInt(board[j][i]+"")-1]==false) b[Integer.parseInt(board[j][i]+"")-1]=true; else return false; } b=new boolean[9]; } for(int x=0;x<7;x+=3){ for(int y=0;y<7;y+=3){ for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ if(board[x+i][y+j]=='.') continue; if(b[Integer.parseInt(board[x+i][y+j]+"")-1]==false) b[Integer.parseInt(board[x+i][y+j]+"")-1]=true; else return false; } } b=new boolean[9]; } } return true; }}
Update 2015/09/30:
public class Solution { public boolean isValidSudoku(char[][] board) { if (board.length !=9 || board[0].length !=9) { return false; } for (int i = 0; i < 9; i++){ boolean[] check = new boolean[9]; for (int j = 0; j < 9; j++){ if (board[i][j] == '.') continue; if (check[(int)(board[i][j] - '1')]){ return false; } check[(int)(board[i][j]-'1')] = true; } } for (int j = 0; j < 9; j++){ boolean[] check = new boolean[9]; for (int i = 0; i < 9; i++){ if (board[i][j] == '.') continue; if (check[(int)(board[i][j]- '1')]){ return false; } check[(int)(board[i][j]-'1')] = true; } } for (int i = 0; i < 9; i=i+3){ for (int j = 0; j < 9; j=j+3){ boolean[] check = new boolean[9]; for (int k = i; k < i+3;k++){ for (int r = j; r < j+3;r++){ if (board[k][r] == '.') continue; if (check[(int)(board[k][r]-'1')]){ return false; } check[(int)(board[k][r]-'1')] = true; } } } } return true; }}
update 2015/10/09: 主要是改变了第三个方格检查,这样就可以使用三个数组一次性扫过检查
public class Solution { public boolean isValidSudoku(char[][] board) { if (board.length !=9 || board[0].length !=9) { return false; } for (int i = 0; i < 9; i++){ boolean[] check = new boolean[9]; for (int j = 0; j < 9; j++){ if (board[i][j] == '.') continue; if (check[(int)(board[i][j] - '1')]){ return false; } check[(int)(board[i][j]-'1')] = true; } } for (int j = 0; j < 9; j++){ boolean[] check = new boolean[9]; for (int i = 0; i < 9; i++){ if (board[i][j] == '.') continue; if (check[(int)(board[i][j]- '1')]){ return false; } check[(int)(board[i][j]-'1')] = true; } } for (int i = 0; i < 9; i++){ boolean[] check = new boolean[9]; for (int j = 0; j < 9; j++){ if (board[3 * (i / 3) + (j / 3)][3 * (i % 3) + (j % 3)] == '.') continue; if (check[board[3 * (i / 3) + (j / 3)][3 * (i % 3) + (j % 3)] - '1']) return false; check[board[3 * (i / 3) + (j / 3)][3 * (i % 3) + (j % 3)] - '1'] = true; } } return true; }}
0 0
- leetcode做题总结,题目Valid Sudoku 2012/03/03
- leetcode做题总结,题目Valid Parentheses 2012/01/30
- leetcode做题总结,题目Longest Valid Parentheses 2012/02/29
- 【leetcode】第36题 Valid Sudoku 题目+解析+代码
- leetcode做题总结,题目Rotate List 2012/03/27
- leetcode做题总结,题目Permutation Sequence 2012/03/27
- leetcode做题总结,题目Anagrams 2012/03/19
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- leetcode做题总结,题目Search Insert Position 2012/03/03
- leetcode做题总结,题目Merge Two Sorted Lists 2012/03/30
- leetcode做题总结,题目Pow(x, n)-------- 2012/03/19
- leetcode做题总结,题目First Missing Positive 2012/03/08
- leetcode做题总结,题目Spiral Matrix I/II 2012/03/24
- leetcode做题总结,题目Search for a Range 2012/03/02
- leetcode做题总结,题目Length of Last Word 2012/03/27
- leetcode做题总结,题目Count and Say 2012/03/05
- HBase查找一条数据的过程
- [poj2777 Count Color]线段树
- zzu oj1223(切割木条)
- EJB初级篇--EJB组件之会话Bean
- ProGuard工具使用
- leetcode做题总结,题目Valid Sudoku 2012/03/03
- 《ORANGE’S:一个操作系统的实现》第一个例子的实现
- java自学之路-----Servlet_过滤器Filter
- EOF是什么?
- NYOJ 733 万圣节派对【简单题】
- 修改目录下 文件 删除字段
- 使用Visual C++进行串口通信编程
- STM32的存储器映射详解
- Princeton Algorithms: Part 2 [week 3: Maxflow and Mincut]