leetcode-- Valid Sudoku
来源:互联网 发布:淘宝买家秀木耳图链接 编辑:程序博客网 时间:2024/06/15 12:35
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
There are just 3 rules to Sudoku.
Each row must have the numbers 1-9 occuring just once.
Each column must have the numbers 1-9 occuring just once.
And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
解:
只循环一次就能检测出结果,生成3个同样大小的bool数组保存检测到的数字,used1 保存每一行中看到的数字,used2 保存每一列,used3保存每一个subblock
class Solution{public: bool isValidSudoku(vector<vector<char> > &board) { int used1[9][9] = {0}, used2[9][9] = {0}, used3[9][9] = {0}; for(int i = 0; i < board.size(); ++ i) for(int j = 0; j < board[i].size(); ++ j) if(board[i][j] != '.') { int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3; if(used1[i][num] || used2[j][num] || used3[k][num]) return false; used1[i][num] = used2[j][num] = used3[k][num] = 1; } return true; }};
0 0
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- LeetCode : Valid Sudoku
- [LeetCode]Valid Sudoku
- [leetcode] Valid Sudoku
- LeetCode-Valid Sudoku
- LeetCode Valid Sudoku
- LeetCode - Valid Sudoku
- LeetCode:Valid Sudoku
- leetcode-036:Valid Sudoku
- Leetcode: Valid Sudoku
- LeetCode题解:Valid Sudoku
- 【LeetCode】 Valid Sudoku
- Leetcode Valid Sudoku
- LeetCode | Valid Sudoku
- LeetCode Valid Sudoku
- [Leetcode] Valid Sudoku (Java)
- Manthan, Codefest 16 -C. Spy Syndrome 2
- 一、 Ubuntu12.04的安装和配置
- MBProgressHUD的基本使用
- 持续集成(CI)、自动化构建和自动化测试--初探
- 谈谈Android中的Divider
- leetcode-- Valid Sudoku
- 数据结构常见面试(1)
- SQL Server 中Inner join 和where的效率差异
- Java类加载器(自定义类加载器)
- window电脑蓝屏
- Android主题切换方案总结
- 十大Intellij IDEA快捷键
- VBA转换文件编码到UTF-8
- c++ primer 课后习题12.24// 12.24.cpp : 定义控制台应用程序的入口点。