LeetCode初级算法-数组-10
2019-01-31 18:36
169 查看
题目:有效的数独
C语言解题
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) { int rowFlag[9][9] = { 0 }; int colFlag[9][9] = { 0 }; int cellFlag[9][9] = { 0 }; int num; for (int i = 0; i < boardRowSize; i++) { for (int j = 0; j < boardColSize; j++) { if (board[i][j] != '.') { num = board[i][j] - '1'; if (rowFlag[i][num] || colFlag[num][j] || cellFlag[i / 3 * 3 + j / 3][num]) return false; rowFlag[i][num] = 1; colFlag[num][j] = 1; cellFlag[3 * (i / 3) + j / 3][num] = 1; } } } return true; }
遍历,使用flag判断行、列、每一个九宫格是否有重复数字。其中i / 3 * 3 + j / 3表示第几个九宫格。
相关文章推荐
- leetcode 初级算法数组8 移动0
- LeetCode(初级算法)数组篇--加一c++
- LeetCode(初级算法)数组篇--存在重复c++
- Leetcode初级算法6两个数组的交集
- LeetCode(初级算法)数组篇-移动零
- LeetCode(初级算法)数组篇--只出现一次的数字c++
- LeetCode初级算法数组9 两数之和
- LeetCode(初级算法)数组篇--两个数组的交集II c++
- LeetCode初级算法-数组-11
- LeetCode初级算法-数组-旋转数组(python)
- LeetCode-探索-初级算法-数组-从排序数组中删除重复项-java
- leetcode 初级算法数组7加一
- leetcode初级算法数组11 旋转图像
- LeetCode(初级算法)数组篇--从排序数组中删除重复项c++
- LeetCode(初级算法)数组篇--买卖股票的最佳时机 II c++
- LeetCode探索模块初级算法数组章节python3代码实现
- LeetCode(初级算法)数组篇--旋转数组c++
- 程序员面试题精选100题(10)-排序数组中和为给定值的两个数字[算法]
- leetcode--中级算法--数组和字符串--递增的三元子序列(JavaScript)
- LeetCode探索模块初级算法字符串章节python3代码实现