【LeetCode】C# 36、Valid Sudoku
来源:互联网 发布:软件结项文档 编辑:程序博客网 时间:2024/05/19 22:46
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.
Sudoku数字游戏。
思路:利用HashSet确保每行每列每大格中的数字不重复。
行:if (board[i, j] != '.' && !rows.Add(board[i, j]))
列:if (board[j, i] != '.' && !columns.Add(board[j, i]))
小九宫格: int RowIndex = 3 * (i / 3);
int ColIndex = 3 * (i % 3);
if (board[RowIndex + j / 3, ColIndex + j % 3] != '.' && !cube.Add(board[RowIndex + j / 3, ColIndex + j % 3]))
public class Solution { public bool IsValidSudoku(char[,] board) { for (int i = 0; i < 9; i++) { HashSet<char> rows = new HashSet<char>(); HashSet<char> columns = new HashSet<char>(); HashSet<char> cube = new HashSet<char>(); for (int j = 0; j < 9; j++) { if (board[i, j] != '.' && !rows.Add(board[i, j])) return false; if (board[j, i] != '.' && !columns.Add(board[j, i])) return false; int RowIndex = 3 * (i / 3); int ColIndex = 3 * (i % 3); if (board[RowIndex + j / 3, ColIndex + j % 3] != '.' && !cube.Add(board[RowIndex + j / 3, ColIndex + j % 3])) return false; } } return true; }}
阅读全文
0 0
- 【LeetCode】C# 36、Valid Sudoku
- [leetcode 36] Valid Sudoku
- leetcode 36: Valid Sudoku
- LeetCode(36) Valid Sudoku
- leetcode-36 Valid Sudoku
- Valid Sudoku - LeetCode 36
- 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
- leetcode-36 Valid Sudoku
- leetcode 36:Valid Sudoku
- LeetCode 36: Valid Sudoku
- [leetcode 36] Valid Sudoku
- 【LEETCODE】36-Valid Sudoku
- AndroidStudio 设置全局查找快捷键
- js对象深拷贝
- Linux中对进程的操作
- vue 插件集合
- Codeforces Round #437 (Div. 1) C
- 【LeetCode】C# 36、Valid Sudoku
- css的overflow属性
- Util-BaseApplication
- 【第五周项目4】数制转换
- PPT制作流程 --- 早看早超生
- 黑客工具软件大全100套
- 合并有序单链表并排序(遍历一次)
- Studio像Eclipse一样设置switch/case那样的代码块
- 搜狗2016研发工程师编程题