Leetcode36.Valid Sudoku有效的数独
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
- 数字 1-9 在每一行只能出现一次。
- 数字 1-9 在每一列只能出现一次。
- 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 '.' 表示。
示例 1:
输入: [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"] ]
输出: true
示例 2:
输入: [
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"] ]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
说明:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 给定数独序列只包含数字 1-9 和字符 '.' 。
- 给定数独永远是 9x9 形式的。
判断3 X 3的方格时,遍历需要每次加3而不是加一
class Solution {
public:
bool isValidSudoku(vector<vector<char> >& board)
{
int len = board.size();
map<char, int> check;
for(int i = 0; i < len; i++)
{
check.clear();
for(int j = 0; j < len; j++)
{
if(board[i][j] == '.')
continue;
if(check[board[i][j]] == 1)
return false;
check[board[i][j]] = 1;
}
check.clear();
for(int j = 0; j < len; j++)
{
if(board[j][i] == '.')
continue;
if(check[board[j][i]] == 1)
return false;
check[board[j][i]] = 1;
}
}
for(int i = 0; i < len - 2; i += 3)
{
for(int j = 0; j < len - 2; j += 3)
{
check.clear();
for(int x = i; x <= i + 2; x++)
for(int y = j ; y <= j + 2; y++)
{
if(board[x][y] == '.')
continue;
if(check[board[x][y]] == 1)
return false;
check[board[x][y]] = 1;
}
}
}
return true;
}
};
Leetcode36.Valid Sudoku有效的数独的更多相关文章
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- leetcode36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode OJ:Valid Sudoku(有效数独问题)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 36 Valid Sudoku(合法的数独)
题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入 ...
- 036 Valid Sudoku 有效的数独
详见:https://leetcode.com/problems/valid-sudoku/description/ class Solution { public: bool isValidSudo ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- valid sudoku(数独)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- Error-Idea:Process finished with exit code 1
ylbtech-Error-Idea:Process finished with exit code 1 1.返回顶部 1. log4j:WARN No appenders could be foun ...
- 观察属性$watch
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- 机器学习中常用的距离及其python实现
1 概述 两个向量之间的距离(此时向量作为n维坐标系中的点)计算,在数学上称为向量的距离(Distance),也称为样本之间的相似性度量(Similarity Measurement).它反映为某类事 ...
- 几种远程调用接口协议简单比较和web service(SOAP)与HTTP接口的区别:
什么是web service? 答:soap请求是HTTP POST的一个专用版本,遵循一种特殊的xml消息格式Content-type设置为: text/xml任何数据都可以xml化. ...
- win8 风格框架
http://metroui.org.ua/挺不错 bootstrap 系列的.
- wc,sort,uniq,awk,grep
wc awk, sort, uniq grep
- Odoo 新 API 概述
__all__ = [ 'Environment', 'Meta', 'guess', 'noguess', 'model', 'multi', 'one', 'cr', 'cr_context', ...
- leetcode 1071 Greatest Common Divisor of Strings
lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...
- Hibernate通用Dao
1. 接口 package com.coder163.main.dao; import org.hibernate.criterion.DetachedCriteria; import java.io ...
- nodejs+express 初学(三)
Nodejs 的模块,nodejs中每一个js文件都是独立的,不用担心他们中的变量会相互覆盖 模块是 Node.js 应用程序的基本组成部分,文件和模块是一一对应的.换言之,一个Node.js 文件就 ...