题目:就是判断已有的数字是否冲突无效,若无效返回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. fullPage 全屏滚动【上下滚动】效果

    由于个人能力,研究了两天,终于写出来了. 又花了一天的时间成功的将30多行脚本,改成了70多行,我也是够有心的了. 那么接下来就是我制作这个效果的全部过程. 那一年我十七,她十八,在那个夏天里,我们, ...

  2. iPad 多任务 Spilt View & Size Class

    iPad 多任务 Spilt View & Size Class 一.多任务简介 iOS 9 以后iPad新增了多任务的支持,主要形式有三种: Slide Over (侧边快捷打开) Spil ...

  3. MongoDB副本集学习(三):性能和优化相关

    Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...

  4. Java并发包源码学习之AQS框架(三)LockSupport和interrupt

    接着上一篇文章今天我们来介绍下LockSupport和Java中线程的中断(interrupt). 其实除了LockSupport,Java之初就有Object对象的wait和notify方法可以实现 ...

  5. findByExample(Object exampleEntity)方法得到的List判断是否为空,不可用(lis != null)

    用findByExample(Object exampleEntity)方法可以应用在用户登录上面,获得有登陆名和密码的user对象进行查询. 返回两者都符合的对象列表,为空则登陆失败. 错误的方法: ...

  6. php:获取浏览器的版本信息

    //分析返回用户网页浏览器名称,返回的数组第一个为浏览器名称,第二个是版本号.  function getBrowser() {      $sys = $_SERVER['HTTP_USER_AGE ...

  7. windows 下wamp环境2 配置之mysql的安装

    安装配置mysql 5.7 打开mysql官网: https://www.mysql.com/ 点击downloads,然后选择commuity 选择MySQL Community Server选择对 ...

  8. SQL Server中TEXT类型字段值在数据库中追加字符串方法

    在数据上我们往往会遇到ntext大文本类型,这种类型如果和 nvarchar类型相加会出现问题,所以有一中方法可以解决这种问题. 使用的sql   函数: TEXTPTR:返回要更新的 text.nt ...

  9. Exploiting the Circulant Structure of Tracking-by-Detection with Kernels(二)

    之前给导师汇报时,主要是论文涉及公式的一些推导

  10. Android活动管理工具

    ActivityCollector.java import android.app.Activity; import java.util.ArrayList; import java.util.Lis ...