LeetCode 36 Valid Sudoku My Submissions Question
2016-03-28 16:18
615 查看
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.每个九宫格square里面的数字不得重复。
2.每一行数字不得重复。
3.每一列数字不得重复。
以上3个条件,需要同时满足,否则数独无效。注意每个square和行列的对应关系。
代码实现如下:
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.每个九宫格square里面的数字不得重复。
2.每一行数字不得重复。
3.每一列数字不得重复。
以上3个条件,需要同时满足,否则数独无效。注意每个square和行列的对应关系。
代码实现如下:
public boolean isValidSudoku(char[][] board) { List<Set<Character>> square = new ArrayList<Set<Character>>(); List<Set<Character>> row = new ArrayList<Set<Character>>(); List<Set<Character>> column = new ArrayList<Set<Character>>(); for (int i = 0; i < 9; i++) { square.add(new HashSet<Character>()); row.add(new HashSet<Character>()); column.add(new HashSet<Character>()); } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (board[i][j] == '.') continue; if (square.get(i / 3 * 3 + j / 3).contains(board[i][j]) || row.get(i).contains (board[i][j]) || column .get(j).contains(board[i][j])) return false; square.get(i / 3 * 3 + j / 3).add(board[i][j]); row.get(i).add(board[i][j]); column.get(j).add(board[i][j]); } } return true; }
相关文章推荐
- 数据加载后刷新UI界面
- 【iOS开发-103】解决方案:iOS8.1中UIBarButtonItem的setTitleTextAttributes对Disabled颜色设置无效
- 浅谈Vue.js
- 解决PKIX:unable to find valid certification path to requested target 的问题
- jsp内置对象request中getParameter方法与get/setAttribute方法的区别
- codeforces_600B. Queries about less or equal elements(排序二分)
- The Ultimate Guide To iPhone Resolutions
- UICollectionview
- mac下CornerstoneSVN出错 Description : The working copy is locked due to a previous error
- no package identifier when getting value for resource number 0x00000005
- 【EJB学习笔记】——JMS和消息驱动Bean
- iOS 自定义UIpageControl
- RequestBodyAdvice和ResponseBodyAdvice
- leetcode 300. Longest Increasing Subsequence
- UI控件--ViewPager详解添加标题栏(3)
- UIView动画(过渡效果)的学习笔记
- easyui datagrid里的toobar按钮隐藏、显示、禁用等方式的实现
- NGUI 层次
- easyui datagrid 让某行复选框置灰不能选
- Your build settings specify a provisioning profile with the UUID, no provisioning profile was