[leetcode] 36. Valid Sudoku 解题报告
来源:互联网 发布:北京正规排名优化企业 编辑:程序博客网 时间:2024/06/05 04:39
题目链接:https://leetcode.com/problems/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.
思路:一个合法的数独不一定是可解的, 因此我们只需要判断是不是给的方格合法就行了, 也就是没有在同一列, 同一行, 或者一个9宫格出现相同的数. 一个十分简便的方法是用位运算的方式, 记录每一行,每 一列, 每一个九宫格的字符相与的结果就一个数保存. 这样如果在某一行列或者宫出现了相同的数的时候我们可以根据并的结果得知. 如果出现了重复的数字则是非法的, 如果到最后仍然找不大重复的数字, 那就是一个合法的数独.
class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { int m = board.size(), n = board[0].size(); vector<int> row(9, 0), col(9, 0), squire(9, 0); for(int i =0; i < m; i++) { for(int j =0; j < n; j++) { if(board[i][j] == '.') continue; int val = 1<<(board[i][j]-'0'); if((row[i]&val) || (col[j]&val) || (squire[i/3*3+j/3]&val)) return false; row[i] |= val, col[j] |= val, squire[i/3*3+j/3] |= val; } } return true; }};参考: https://leetcode.com/discuss/92445/yet-another-java-2ms-solution
0 0
- [leetcode] 36. Valid Sudoku 解题报告
- [Leetcode] 36. Valid Sudoku 解题报告
- [leetcode]36. Valid Sudoku@Java解题报告
- [LeetCode] Valid Sudoku 解题报告
- Leetcode #36. Valid Sudoku 数独游戏验证 解题报告
- 【LeetCode】Valid Sudoku解题笔记
- [LeetCode]Valid Sudoku解题记录
- LeetCode---Valid Sudoku解题分析
- [leetcode javascript解题]Valid Sudoku
- [LeetCode]36.Valid Sudoku
- LeetCode --- 36. Valid Sudoku
- LeetCode 36.Valid Sudoku
- [Leetcode] 36. Valid Sudoku
- 【leetcode】36. Valid Sudoku
- [leetcode] 36. Valid Sudoku
- Leetcode 36. Valid Sudoku
- 36. Valid Sudoku LeetCode
- leetcode 36. Valid Sudoku
- 程序员生存定律--成长路上常见的坑
- 设置滑出菜单
- Linux流量监控工具 - iftop
- Notification的使用简单的介绍
- BZOJ1997 2-sat
- [leetcode] 36. Valid Sudoku 解题报告
- FastJSON 简介及其Map/JSON/String 互转
- C中多线程开发
- 负载均衡技术
- 360/IE兼容模式下。append 失效,ajax返回json,传值为0问题
- html中JavaScript实现两个子节点互换
- iOS No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, V
- 高效加载Bitmap(Displaying Bitmaps Efficiently)
- 自动化脚本ssh以及telnet发送命令并退出(windows和linux都适用)