leetcode 36 Valid Sudoku C++
2016-06-02 16:17
375 查看
判断数独是不是正确的,就是判断每一行,每一列,每个小棋盘,有没有重复的。
用三个二维数组纪录。有就置1.
主要的是小棋盘序号的计算方法。
行为i/3*3
列为j/3
小棋盘的序号为行加列。
(参考了discuss里面最热的帖子)
代码如下:
bool isValidSudoku(vector<vector<char>>& board) {
int board1[9][9] = {0};
int board2[9][9] = {0};
int board3[9][9] = {0};
for(int i = 0;i<9;i++)
for(int j = 0;j<9;j++){
if(board[i][j]!='.'){
int num = board[i][j] - '0' - 1;
int k = i/3*3+j/3;
if(board1[i][num]||board2[j][num]||board3[k][num])
return false;
board1[i][num]=board2[j][num]=board3[k][num]=1;
}
}
return true;
}
用三个二维数组纪录。有就置1.
主要的是小棋盘序号的计算方法。
行为i/3*3
列为j/3
小棋盘的序号为行加列。
(参考了discuss里面最热的帖子)
代码如下:
bool isValidSudoku(vector<vector<char>>& board) {
int board1[9][9] = {0};
int board2[9][9] = {0};
int board3[9][9] = {0};
for(int i = 0;i<9;i++)
for(int j = 0;j<9;j++){
if(board[i][j]!='.'){
int num = board[i][j] - '0' - 1;
int k = i/3*3+j/3;
if(board1[i][num]||board2[j][num]||board3[k][num])
return false;
board1[i][num]=board2[j][num]=board3[k][num]=1;
}
}
return true;
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [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
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- 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 题解