这道题目被放在的简单的类别里是有原因的,题目如下:

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

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

A partially filled sudoku which is valid.

Note:

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

又是一开始犯了审题错误的毛病,我以为是要写出一个计算数独的AI,还在想为啥这种难度都被认为成简单,结果我们看最后的note:

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

就是说不需要这个数独是可以解,只要判断给你的这个数独题目是不是合法的数独就行了。所以难度就骤降了,随便找了一个解法上去AC。

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
vector<vector<int>> bucket(3, vector<int>(9, 0));
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (board[i][j] != '.' && ++bucket[0][board[i][j] - '1'] > 1) return false;
if (board[j][i] != '.' && ++bucket[1][board[j][i] - '1'] > 1) return false;
int v = j%3 + 3*(i%3);
int h = j/3 + 3*(i/3);
if (board[v][h] != '.' && ++bucket[2][board[v][h] - '1'] > 1) return false;
}
bucket = vector<vector<int>>(3, vector<int>(9, 0));
}
}
};

每个点的合法性判断有三种,一种是横向是否有相同数字,一种是纵向是否有相同数字,一种是这个点所在的3*3格子内是否用相同数字。

[leetcode] 20. Valid Sudoku的更多相关文章

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

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

  2. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  3. LeetCode 36 Valid Sudoku

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

  4. 【leetcode】Valid Sudoku

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

  5. Java [leetcode 36]Valid Sudoku

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

  6. LeetCode(38)-Valid Sudoku

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

  7. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  8. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

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

  9. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

随机推荐

  1. Manta

    安装python客户端: pip install manta import manta as pymanta# cat ${MANTA_PRIVATE_KEY_PATH} | tr '\n' '#' ...

  2. 常用快捷键及eclipise快捷键

    win+R 运行...win+D 桌面win+E 打开我的电脑win+F 搜索 ctrl+D删除光标所在行

  3. java内存模型:简单理解

    1.Java内存模型(Java Memory Model,JMM) 2.JMM定义了线程和主内存之间的抽象关系: 线程之间的共享变量存储在主内存(main memory)中,每个线程都有一个私有的本地 ...

  4. 43. Multiply Strings (String)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. C++中的浅拷贝和深拷贝

    浅拷贝(shallow copy)与深拷贝(deep copy)对于值拷贝的处理相同,都是创建新对象,但对于引用拷贝的处理不同,深拷贝将会重新创建新对象,返回新对象的引用字.浅拷贝不会创建新引用类型. ...

  6. iOS下JS与OC互相调用(七)--Cordova 环境搭建

    Cordova大家可能比较陌生,但肯定听过 PhoneGap ,Cordova 就是 PhoneGap 被 Adobe 收购后所改的名字.它是一个可以让 JS 与原生代码互相通信的一个库,并且提供了一 ...

  7. 用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn't match any of the subject alternative names问题的解决

    最近用server酱-PushBear做消息自动推送,用apache HttpClient做https的get请求,但是代码上到服务器端就报javax.net.ssl.SSLException: Ce ...

  8. MongoDB的文档、集合、数据库(二)

    为了理解MongoDB的名词,可以将其于关系型数据库进行对比: 一.文档 概述 文档是MongoDB的核心概念,是数据的基本单元,非常类似于关系数据库中的行.在MongoDB中,文档表示为键值对的一个 ...

  9. php Pthread 多线程 (三) Mutex 互斥量

    当我们用多线程操作同一个资源时,在同一时间内只能有一个线程能够对资源进行操作,这时就需要用到互斥量了.比如我们对同一个文件进行读写操作时. <?php class Add extends Thr ...

  10. 模板练习(LUOGU)

    1:并查集 P3183食物链 #define man 300050 ; int find(int x){ if(fa[x]==x) return fa[x]; return fa[x]=find(fa ...