【LeetCode從零單排】No36 Valid Sudoku
題目
判斷數獨是否成立的一道題,看的是某大神的答案,寫的太漂亮了。
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.
程式碼
public class Solution {
public boolean isValidSudoku(char[][] board) {
int count = 9;
int blockWidth = 3;
for (int i=0; i<count; i++) {
boolean[] rowExist = new boolean[count+1];
boolean[] colExist = new boolean[count+1];
boolean[] matrixExist = new boolean[count+1];
for (int j=0; j<count; j++) {
int rowNum = board[i][j] == '.' ? -1 : board[i][j] - '0';
int colNum = board[j][i] == '.' ? -1 : board[j][i] - '0';
int mtxRowIdx = 3*(i/3);
int mtxColIdx = 3*(i%3);
int matrixNum = board[mtxRowIdx + j/3][mtxColIdx + j%3] == '.' ?
-1 : board[mtxRowIdx + j/3][mtxColIdx + j%3] - '0';
if (rowNum > 0 && rowExist[rowNum] ||
colNum > 0 && colExist[colNum] ||
matrixNum > 0 && matrixExist[matrixNum]) {
return false;
}
if (rowNum > 0)
rowExist[rowNum] = true;
if (colNum > 0)
colExist[colNum] = true;
if (matrixNum > 0)
matrixExist[matrixNum] = true;
}
}
return true;
}}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- Leetcode Valid SudokuLeetCode
- Leetcode 36 Valid SudokuLeetCode
- Leetcode-Valid SudokuLeetCode
- Valid Sudoku leetcode javaLeetCodeJava
- [LeetCode] Valid Sudoku 驗證數獨LeetCode
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- 【LeetCode 36_雜湊表】Valid SudokuLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- leetCode 36. Valid Sudoku(數獨) 雜湊LeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No70.ClimbingStairsLeetCodeAI
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- 【LeetCode從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Mysql從零單排-1MySql
- 【LeetCode從零單排】No96 Unique Binary Search TreesLeetCode
- 【LeetCode從零單排】No112 Path SumLeetCode
- 【LeetCode從零單排】No.169 Majority Element(hashmap用法)LeetCodeHashMap
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 從零單排學Redis【白銀】Redis
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【LeetCode從零單排】No129 Sum Root to Leaf NumbersLeetCode
- 【LeetCode從零單排】No.160 Intersection of Two Linked ListsLeetCode
- 從零單排學Redis【黃金】Redis
- 【LeetCode從零單排】No121 Best Time to Buy and Sell StockLeetCode
- 【LeetCode從零單排】No118 Pascal's TriangleLeetCode
- 【LeetCode從零單排】No104 Maximum Depth of Binary TreeLeetCode