Leetcode[36]-Valid Sudoku
来源:互联网 发布:小程序跳过域名校验 编辑:程序博客网 时间:2024/06/02 23:52
Link: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.
分析:验证一个数独的合法性,从三个方向入手
- 每一行不能出现重复元素
- 每一列不能出现重复元素
- 每一个九方格不能出现重复元素
我采用的是map来保存元素的数值,然后判断map中是否有,有则返回false;参数之间的关系推导如下:
代码如下
Code(c++):
class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { //row for(int i = 0; i<9; i++) { map<int, int> mp; for (int j = 0; j<9; j++){ if(mp.find(board[i][j])!=mp.end()) return false; if(board[i][j] == '.')continue; mp[board[i][j]] = j; } } //cow for(int j = 0; j<9; j++) { map<int, int> mp; for (int i = 0; i<9; i++){ if(mp.find(board[i][j])!=mp.end()) return false; if(board[i][j] == '.')continue; mp[board[i][j]] = i; } } //board for(int i = 0; i < 9; i++){ map<int,int> mp; for(int j = (i/3)*3; j < 3 + (i/3)*3 ; j++){ for(int k = (3*i)%9; k <= (3*(i+1)-1)%9; k++){ if(mp.find(board[j][k])!=mp.end()) return false; if(board[j][k] == '.')continue; mp[board[j][k]] = k; } } } return true; }};
0 0
- [leetcode 36] Valid Sudoku
- leetcode 36: Valid Sudoku
- LeetCode(36) Valid Sudoku
- leetcode-36 Valid Sudoku
- Valid Sudoku - LeetCode 36
- 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
- leetcode-36 Valid Sudoku
- leetcode 36:Valid Sudoku
- LeetCode 36: Valid Sudoku
- [leetcode 36] Valid Sudoku
- 【LEETCODE】36-Valid Sudoku
- Leetcode 36 - Valid Sudoku
- 虚拟机上网
- 如何在MyEclipse上耍Chrome
- 隐马尔科夫七(二)
- Java增强循环
- 改变医疗行业的八个大数据应用
- Leetcode[36]-Valid Sudoku
- Eclipse中通过SVN安装Spring源代码环境遇到的问题
- sina33打开spi0dev0.0、spi1dev1.0的步骤.txt(全志a33下生成设备节点/dev/spi0dev0.0、/dev/spi1dev1.0)
- 修改文件属主和属组命令chown、chgrp
- SpringMVC 生成Excel和PDF
- Android官方培训课程中文版(v0.9.3)
- 我的奋斗之我的构想
- leetcode013
- hdoj 3572 Task Schedule 【最大流 在时间区间建图判断是否满流】