题目:就是判断已有的数字是否冲突无效,若无效返回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. JCarouselLite--帮助文档

    jcarousellite是一款jquery插件,可以控制文档元素滚动,丰富的参数设置可以控制滚动的更多细节,是一款不可多得的滚动插件. ------------------ 官网地址:http:// ...

  2. iOS resign code with App Store profile and post to AppStore

    http://stackoverflow.com/questions/17545452/ios-resign-code-with-app-store-profile-and-post-to-appst ...

  3. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  4. mongodb的sql例子(简单版)

    插入数据 db.person.insert({"name":"zfx","age":21}) 查找所有数据 db.person.find() ...

  5. mysql 在linux 修改账号密码

    1.root用户登录到mysql数据库代码示例:/usr/local/mysql/bin/mysql -u root -p (输入密码进入mysql)2.进入mysql,输入:代码示例:use mys ...

  6. js图文讲解

       

  7. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. web安全及防护

    本文将对web方面的安全问题以及相应的防护方法进行一个简单的介绍. SQL注入(SQL Injection) 原理:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺 ...

  9. 使用EasyBCD完美实现Windows7与Linux双系统

    在Linux横行的时代,不玩Linux简直对不起它的流行,但又不能不用Windows,还要干活,还要工作啊,最重要的是:还要娱乐啊!! 废话不多说,直接进入主题! 1.下载EasyBCD,这个软件可以 ...

  10. Internet与www的关系

    Internet是把分布于世界各地不同结构的计算机网络用各种传输介质相互连接起来的网络. 因此,被称为网络的网络.Internet提供的主要服务有万维网(WWW.)文件传输(FTP.)电子邮件(E-m ...