题目:就是判断已有的数字是否冲突无效,若无效返回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)的更多相关文章

  1. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  2. 【LeetCode】 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  3. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  4. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  5. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  6. 【leetcode】Valid Parentheses

    题目简述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  7. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  8. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

随机推荐

  1. BSA基础数据维护

    平台 BSA基础数据维护 .扇区五个字段的内容 本来值为0,经过107上计算解析,得出正常的数值.然后106上报(200050),得到回复(200051). 查看回复数据,是否有错误.比如提示104 ...

  2. <s:url>指向的Action只执行一次,清除浏览器缓存文件后又可执行一次。

    Action中的方法仅为静态变量赋值,而其他访问数据库的Action可以被重复执行. 起初判断可能是静态变量的内存机制导致不能重复执行. 然后发现清楚浏览器缓存文件后又可以执行一次了,看来原因在Jsp ...

  3. 2015多校.Zero Escape (dp减枝 && 滚动数组)

    Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  4. oracle数据表创建分区与查询

    场景: 遇到1亿数据量的数据需要根据用户名做些数据统计分析,想直接做些聚合计算基本没可能,于是打算先根据日期按照年月创建分区,然后对各个分区分别进行统计,最后汇总结果. 有两种方法,分别是手工设置分区 ...

  5. HTML5 video 视频标签 常用属性

    最近在做手机端的 h5 页面的视频直播功能,用到了 Video 标签.其常用的属性有以下几个: src.poster.preload.autoplay.loop.controls.width.heig ...

  6. Java网络编程学习

    服务器是指提供信息的计算机或程序,客户机是指请求信息的计算机或程序,而网络用于连接服务器与客户机,实现两者相互通信.但有时在某个网络中很难将服务器与客户机区分开.我们通常所说的“局域网”(Local ...

  7. Mongodb——GridFS

    GridFS用于存储和恢复那些超过16M(BSON文件限制)的文件. GridFS将文件分成大块,将每个大块存储为单独的文件.GridFS中限制chunk最大为256k.GridFS使用两个colle ...

  8. css3 github javascript

    欢迎关注我的 github github博客地址 github地址

  9. 微软“One Windows”的梦想已经破灭了吗?

    导读 Windows 10 正式公布的时候,微软曾表示该系统将开启更为个性化的计算新纪元,可让用户在使用各类设备处理各项事务时,享受到一致.熟悉和可兼容的体验,从 Xbox 到 PC 和手机,再到平板 ...

  10. python扩展实现方法--python与c混和编程 转自:http://www.cnblogs.com/btchenguang/archive/2012/09/04/2670849.html

    前言 需要扩展Python语言的理由: 创建Python扩展的步骤 1. 创建应用程序代码 2. 利用样板来包装代码 a. 包含python的头文件 b. 为每个模块的每一个函数增加一个型如PyObj ...