【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 ...
随机推荐
- .NET中的工作目录一览!
定义: 当前工作目录——进行某项操作的目的目录,会随着OpenFileDialog.SaveFileDialog等对象所确定的目录而改变. 当前执行目录——该进程从中启动的目录,即文件自身 ...
- spring 缓存(spring自带Cache)(入门)
spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...
- ASP.NET 生成报表的几中方案
1. 用html 表格绘制报表,javascript导出EXCEL 2. 采用datagrid绑定报表数据,用后台方法导出 //Response.AppendHeader("Content- ...
- HDOJ 4497 GCD and LCM
组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- 7-RandomAccessFile 随机流
package com.io; import java.io.File; import java.io.FileNotFoundException; import java.io.IOExceptio ...
- 请求WebMethod, Ajax 处理更加专注
在WebForm下 开发ajax程序,需要借助于一般处理程序(*.ashx)或web服务(*.asmx),并且每一个ajax请求,都要建一个这样的文件,如此一来,如 果在一个项目中ajax程序多了,势 ...
- java之stream(jdk8)
一.stream介绍 参考: Java 8 中的 Streams API 详解 Package java.util.stream Java8初体验(二)Stream语法详解 二.例子 im ...
- webApp前端开发技巧总结
自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于WEB形式的应用程序,运行在高端的移动终端设备.我相信各位童鞋应该和我一个样子, ...
- Extjs 学习笔记1
学习笔记 目 录 1 ExtJs 4 1.1 常见错误处理 4 1.1.1 多个js文件中有相同的控件,切换时无法正常显示 4 1.1.2 Store的使用方法 4 1.1.3 gridPanel ...
- CSS 继承深度解析
FROM ME: 之前在研究前端性能优化的时候,就有学习关于CSS中“善用CSS中的继承”. 原文:CSS Inheritance, The Cascade And Global Scope: You ...