leetcode-algorithms-36 Valid Sudoku
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:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- 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-36 Valid Sudoku的更多相关文章
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
- 【LeetCode】36 - Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...
- 【一天一道LeetCode】#36. Valid Sudoku
一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...
- 【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 ...
- LeetCode:36. Valid Sudoku(Medium)
1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
随机推荐
- GET和POST中文乱码的解决方法
如果表单中含有中文,采用GET或者POST提交请求时,getParameter()方法接收到的参数值乱码. 1.乱码产生的原因 请求参数通过浏览器发送给Tomcat服务器,浏览器发送编码,但是tomc ...
- 牛客练习赛7 E 珂朵莉的数列(树状数组+爆long long解决方法)
https://www.nowcoder.com/acm/contest/38/E 题意: 思路: 树状数组维护.从大佬那里学习了如何处理爆long long的方法. #include<iost ...
- oracle 新建用户
create user username identified by password; grant dba to username; 授权 或 grant connect, resource to ...
- 【Django】Django-REST-Framework
[创建简单的API] 1. cmd.exe >django-admin startproject django_rest>cd django_rest\django_rest>pyt ...
- docker+jenkins实现spring boot项目持续集成自动化部署
一.首先jenkins与docker的安装参考下面链接 安装jenkins: https://www.cnblogs.com/jescs/p/7644635.html 安装docker:ht ...
- memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
1.在http://libevent.org/ 下载libevent-2.0.22-stable.tar.gz 2.tar -zxvf libevent-2.0.22-stable.tar.gz ...
- java常用技术名词解析
1.1 token Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便 将此Token返回给客户端,以后客户端只需带上这个Token前来请求数 ...
- tomcat热部署.class
本人是在维护公司系统时遇到的问题,由于公司的系统是部署到客户服务器上,而系统中存在的问题又比较多,需要经常维护.如果每次修改完class文件后都需要去重启服务器, 那会给用户的使用造成不便,所以需要使 ...
- react面试问题总结
1. 在生命周期中的哪一步你应该发起 AJAX 请求? 我们应当将AJAX 请求放到 componentDidMount 函数中执行,主要原因有下: 放到componentWillMount不好. ...
- PostCSS以及cssnext语法
什么是postcss postcss 一种对css编译的工具,类似babel对js的处理,常见的功能如: 1 . 使用下一代css语法 2 . 自动补全浏览器前缀 3 . 自动把px代为转换成 rem ...