Leetcode -- Valid Sudoku
2015-08-23 18:01
295 查看
题目:
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.
分析:
规则很简单,不要有重复的数字就好。遍历一遍,是必需的,也只能遍历一遍。复杂度高了,肯定不过不了。
代码:
注意:
下标什么时候结束的问题。第一次刷的时候,下标的边界弄错了,调试了好久,sad!
一旦涉及到循环,下标的边界一定要好好检查一下。
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.
分析:
规则很简单,不要有重复的数字就好。遍历一遍,是必需的,也只能遍历一遍。复杂度高了,肯定不过不了。
代码:
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { vector< vector<int> > arr(10, vector<int>(10, 0)); vector<vector<int>> smallArr(10, vector<int>(10, 0)); vector<vector<int>> col(10, vector<int>(10, 0)); int num; for(int i = 0; i<= 6; i = i + 3) { for(int j = 0; j<= 6; j = j + 3) { for(int a = 0; a< 3; a++) { for(int b =0; b<3; b++) { if(board[i+a][j+b] != '.') { num = board[i+a][j+b] - '0'; arr[i +a][num] ++; smallArr[i + j/3][num] ++; col[j + b][num] ++; if(arr[i+a][num] > 1 || smallArr[i + j/3][num]> 1 || col[j + b][num] > 1) { return false; } } } } } } return true; } };
注意:
下标什么时候结束的问题。第一次刷的时候,下标的边界弄错了,调试了好久,sad!
一旦涉及到循环,下标的边界一定要好好检查一下。
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- VFP Control - When | Valid
- extjs form model vilidations
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解