leetcode-algorithms-36 Valid Sudoku

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

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

Example 1:

Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true

Example 2:

Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

解法

将行,列和小方框三个全都各自存到一个二维数组中,第一维存位置,第二维数值,并将结果置1.如果已在当前数组中有重复的数值,其值会是1.如例子2: board[0][0]位置有数值8,则column[0][7]=1,在board[3][0]位置还有个数值8同,则重复了column[0][7],验证失败.

class Solution
{
public:
bool isValidSudoku(vector<vector<char>>& board)
{
int row[9][9] = {0};
int column[9][9] = {0};
int sub_box[9][9] = {0}; for (int i = 0; i < board.size(); ++i)
{
for (int j = 0; j < board[i].size(); ++j)
{
if (board[i][j] != '.')
{
int num = board[i][j] - '0' - 1;
int sub_index = i / 3 * 3 + j / 3;
if (column[i][num] > 0 || row[j][num] > 0 || sub_box[sub_index][num] > 0)
return false;
column[i][num] = row[j][num] = sub_box[sub_index][num] = 1;
}
}
}
return true;
}
};

时间复杂度: O(n^2).

空间复杂度: O(3*n^2).

链接: leetcode-algorithms 目录

leetcode-algorithms-36 Valid Sudoku的更多相关文章

  1. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  2. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  3. 【LeetCode】36 - Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...

  4. 【一天一道LeetCode】#36. Valid Sudoku

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...

  5. 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)

    Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...

  6. LeetCode:36. Valid Sudoku(Medium)

    1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...

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

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

  8. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  9. [LeetCode] 36. Valid Sudoku 验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  10. LeetCode 36 Valid Sudoku

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

随机推荐

  1. 6、lvs使用进阶(02)

    把web server服务和443服务绑定在一起之后呢? 假设一种场景,对web服务器来讲需要session保持.一个在线购物网站,在购物时,如果不结账,一般是http协议,当结账时,需要网站跳转,可 ...

  2. 【译】第23节---Fluent API - 实体映射

    原文:http://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx ...

  3. vue动态改变样式

    <i class="el-icon-arrow-left" :style="{'color': deadColorArr[index]}" @click= ...

  4. vs里32位项目和64位项目的区别

    由于操作系统内存分配的不同,导致软件开发过程中,需要编译不同版本的软件. 1.编译程序根据需要选择不同的编译环境. x86和win32为32位程序,x64为64位程序,可以选择不同的编译条件形成不同位 ...

  5. Vue--vux组件库

    各种组件demo源码~ https://doc.vux.li/zh-CN/

  6. ros topic 发布一次可能会接收不到数据

    rostopic pub - /hdw_update hdw_driver/update_file_msg A B C D 系统提示: publishing and latching message ...

  7. Springboot Thymeleaf 发邮件 将html内容展示在邮件内容中

  8. 虚拟机安装---vm12+ubuntukylin16.04

    虚拟机安装及部分问题解决方案: https://blog.csdn.net/qq_35208390/article/details/78271743 安装vmware tools后,需设置共享文件夹才 ...

  9. vscode所用插件

  10. 记录Python类与继承的一个错误

    今天在学python的类与继承的时候遇到一个错误,原来是自己在ctrl+c  ctrl+v的时候漏了一个括号 class Car(): def __init__(self,make,year,mode ...