LeetCode 36. Valid Sudoku (C++)
题目:
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9without repetition. - Each column must contain the digits
1-9without repetition. - Each of the 9
3x3sub-boxes of the grid must contain the digits1-9without repetition.
![]()
分析:
每一行,每一列没有相同的数字,每一个3*3的小正方形中也没有相同的数字。可以使用数组,map,set等等来判断。
程序:
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
map<char,int> m;
for (int i = ; i < ; i++){
for (int j = ; j < ; j++){
if (board[i][j] != '.'){
if (m[board[i][j]]) return false;
m[board[i][j]]++;
}
}
m.clear();
}
for (int i = ; i < ; i++){
for (int j = ; j < ; j++){
if (board[j][i] != '.'){
if (m[board[j][i]]) return false;
m[board[j][i]]++;
}
}
m.clear();
}
for (int i = ; i < ; i++){
for (int j = ; j < ; j++){
for (int r = i*; r < i*+; r++){
for (int l = j*; l < j*+; l++){
if (board[r][l] != '.'){
if (m[board[r][l]]) return false;
m[board[r][l]]++;
}
}
}
m.clear();
}
}
return true;
}
};
LeetCode 36. Valid Sudoku (C++)的更多相关文章
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- [leetcode]36. Valid Sudoku验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- LeetCode 36. Valid Sudoku (Medium)
题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...
- LeetCode 36 Valid Sudoku(合法的数独)
题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入 ...
随机推荐
- let与var的区别,为什么什么要用let?
1.var是全局声明,let是块级作用的,只适用于当前代码块 var a = 1: if(true){ let a; a=22: console.log(a);'//22 } if(){}内就是let ...
- Python 基础 Dict 和 Set 类型
python 什么是dict 例如: d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } 我们把名称称为key,对应的成绩称为value,dic就是通过key 来查找 ...
- js怎样得出数组中某个数据最大连续出现的次数
1:js怎样得出数组中某个数据最大连续出现的次数 var test=[1,2,3,3,2,2,2,3,3,3,3,5,3,3,3,3,3] ; var j = 0 ; var max ...
- IO流C++
1.iostream处理控制台IO #include<iostream> #include<string> using namespace std; istream& ...
- 如何使用gitbash 把你的代码托管到github
1.如果你没有创建仓库 mkdir vuex-playList cd vuex-playList git init touch README.md git add README.md git comm ...
- PhpStorm 查看当前类中所有的方法
展示当前类中的所有方法 Ctrl + F12 方法之间移动 alt + 向上箭头/向下箭头
- 关于Xshell无法连接本地虚拟机的问题
近期想搭建一个测试用的集群,但是! 刚开始搭第一台虚拟机就出现问题了,Xshell无法连接到虚拟机! 然后我更改了/etc/sysconfig/network-scripts/ifcfg-ens33 ...
- CAP通俗解释
CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),这三个基本需求,最多只 ...
- ACM1008:Elevator
Problem Description The highest building in our city has only one elevator. A request list is made u ...
- docker 容器 设置网络代理
以/bin/bash 形式进入容器: [设置http 及https代理],如下: export http_proxy=http://172.16.0.20:3128 export https_prox ...