【leetcode】Valid Sudoku (easy)
题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解
用了char型的数字,并且空格用‘.'来表示的。
思路:只要分别判断横向 竖向 3*3小块中的数字是否有重复或者无效就可以了 就是单纯的麻烦 不难
#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
vector<int> hash;
//纵向判断
for(int c = ; c < ; c++) //对每一列判断
{
hash.clear();
hash.resize(); //每次判断前 hash要清零
for(int r = ; r < ; r++)
{
if(board[r][c] == '.') //空白跳过
{
continue;
}
else if(board[r][c] <= '' && board[r][c] >= '') //是数字
{
if(hash[board[r][c] - ''] != ) //若已经有过 无效
{
return false;
}
else
{
hash[board[r][c] - '']++;
}
}
else //不是空白 不是数字 无效
{
return false;
}
}
}
//横向判断
for(int r = ; r < ; r++) //对每一行判断
{
hash.clear();
hash.resize();
for(int c = ; c < ; c++)
{
if(board[r][c] == '.')
{
continue;
}
else if(board[r][c] <= '' && board[r][c] >= '')
{
if(hash[board[r][c] - ''] != )
{
return false;
}
else
{
hash[board[r][c] - '']++;
}
}
else
{
return false;
}
}
}
//小矩形判断
hash.clear();
hash.resize();
for(int r = ; r < ; r+=) //对每一行判断
{
for(int c = ; c < ; c+=)
{
hash.clear();
hash.resize();
for(int rr = r; rr < r + ; rr++)
{
for(int cc = c; cc < c + ; cc++)
{
if(board[rr][cc] == '.')
{
continue;
}
else if(board[rr][cc] <= '' && board[rr][cc] >= '')
{
if(hash[board[rr][cc] - ''] != )
{
return false;
}
else
{
hash[board[rr][cc] - '']++;
}
}
else
{
return false;
}
}
}
}
}
return true;
}
}; int main()
{
Solution s; vector<vector<char>> sudo;
vector<char> sub;
char c;
for(int i = ; i < ; i++)
{
sub.clear();
for(int j = ; j < ; j++)
{
scanf("%c ", &c);
sub.push_back(c);
}
sudo.push_back(sub);
} bool b = s.isValidSudoku(sudo); return ;
}
【leetcode】Valid Sudoku (easy)的更多相关文章
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- 【LeetCode】 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Valid Parentheses
题目简述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode】- Valid Palindrome(右回文)
[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...
随机推荐
- embed 层级太高
它怎么就好了,凭什么就好了.为什么就好了.我到底当时是哪里写错了.怎么个情况 兼容Firefox ,IE的flash透明和flash置底代码 <object classid="clsi ...
- 新手使用R的注意事项
1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...
- gulp常用插件
本人就职于一家互联网小公司,根据自己的项目纪录一下常用的gulp插件,项目不是很大! 文章目录 1. gulp-load-plugins 2. yargs 3. del 4. gulp-filter ...
- 安装sqlserver2012时出现的丧心病狂的错误
Service Pack 安装程序 ------------------------------ 出现以下错误: 安装程序集“Microsoft.VC80.ATL,version="8.0. ...
- navigationcontroller手势翻页和navigationbar
一. 系统导航默认手势 #import "CBNavigationController.h" //手势返回 @interface CBNavigationController () ...
- [转载]JavaEE学习篇之——JQuery技术详解
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...
- 160809228_符瑞艺_C语言程序设计实验3 循环结构程序设计
#include <stdio.h> int main(){ //使用for循环完成1+2+......+100 ; ;i<=;i++) sum +=i; //sum = sum ...
- NOI2005 聪聪和可可
Sol 记忆化搜索. \(f[u][v]\) 表示聪聪在 \(u\) ,可可在 \(v\) ,聪聪抓到可可的期望. 预处理出 \(u\) 到 \(v\) 最短路径编号最小的点,记为 \(g[u][v] ...
- windows下nodejs常见错误
1.express-session express-session deprecated undefined resave option; provide resave option auth_s e ...
- 2-python学习——hello world
"hello world"是编程界一个经久不衰的例子,几乎所有语言的学习教程都把它当做第一个程序的范例.学习的过程就是再造轮子的过程,千万不要以为有人做过的,就不去学习了. hel ...