LeetCode Online Judge 题目C# 练习 - Valid Sudoku
2012-10-23 03:45
525 查看
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 '.'.
代码分析:
不就是前面一题的helper function吗?
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
public static bool ValidSudoku(List<List<char>> board) { for (int i = 0; i < board.Count; i++) { for (int j = 0; j < board[0].Count; j++) { if (board[i][j] != '.') if (!isValid(board, i, j)) return false; } } public static bool isValid(List<List<char>> board, int r, int c) { //////////////CHECK ROW///////////////////// bool[] Row = new bool[9]; for (int i = 0; i < 9; i++) { if (board[r][i] >= '1' && board[r][i] <= '9') { if (Row[board[r][i] - '1'] == false) Row[board[r][i] - '1'] = true; else return false; } } /////////////CHECK COLUMN////////////////// bool[] Col = new bool[9]; for (int i = 0; i < 9; i++) { if (board[i][c] >= '1' && board[i][c] <= '9') { if (Col[board[i][c] - '1'] == false) Col[board[i][c] - '1'] = true; else return false; } } /////////////CHECK GRID/////////////////// bool[] Grid = new bool[9]; // r / 3 * 3 = beginning row number of that grid // c / 3 * 3 = beginning column number of that grid for (int i = (r / 3) * 3; i < (r / 3) * 3 + 3; i++) { for (int j = (c / 3) * 3; j < (c / 3) * 3 + 3; j++) { if (board[i][j] >= '1' && board[i][j] <= '9') { if (Grid[board[i][j] - '1'] == false) Grid[board[i][j] - '1'] = true; else return false; } } } return true; }
代码分析:
不就是前面一题的helper function吗?
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - ZigZag Conversion
- LeetCode Online Judge 题目C# 练习 - Gray Code
- LeetCode Online Judge 题目C# 练习 - Implement strStr()
- LeetCode Online Judge 题目C# 练习 - Add two number
- LeetCode Online Judge 题目C# 练习 - Longest Substring Without Repeating Characters
- LeetCode Online Judge 题目C# 练习 - Reverse Nodes in k-Group
- LeetCode Online Judge 题目C# 练习 - 4SUM
- LeetCode Online Judge 题目C# 练习 - Binary Tree Level Order Traversal II
- LeetCode Online Judge 题目C# 练习 - Combination
- LeetCode Online Judge 题目C# 练习 - N-Queens
- LeetCode Online Judge 题目C# 练习 - Add Binary
- LeetCode Online Judge 题目C# 练习 - Length of Last Word
- LeetCode Online Judge 题目C# 练习 - Two Sum
- LeetCode Online Judge 题目C# 练习 - Count and Say
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - Remove Element
- LeetCode Online Judge 题目C# 练习 - Decode Way
- LeetCode Online Judge 题目C# 练习 - Plus One
- LeetCode Online Judge 题目C# 练习 - Search Insert Position
- LeetCode Online Judge 题目C# 练习 - Unique Binary Search Trees II