LeetCode OJ:Valid Sudoku(有效数独问题)
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 '.'
.
注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了。
原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下别人的,很精妙,代码如下:
1 class Solution {
2 public:
3 bool isValidSudoku(vector<vector<char>>& board) {
4 vector<vector<bool>> rowValid(9, vector<bool>(9, true));
5 vector<vector<bool>> colValid(9, vector<bool>(9, true));
6 vector<vector<bool>> blockValid(9, vector<bool>(9, true));
7 for (int i = 0; i < 9; ++i){
8 for(int j = 0; j < 9; ++j){
9 if(board[i][j] == '.') continue;
10 int num = board[i][j] - '1';//注意这里是减'1'
11 if(!rowValid[i][num] || !colValid[j][num] || !blockValid[i-i%3+j/3][num])//判定在一个block中的代码很巧妙,i-i%3制造了一个高度为3的阶梯,然后j/3正好可以将这个阶梯填满
12 return false;
13 rowValid[i][num] = colValid[j][num] = blockValid[i-i%3+j/3][num] = false;
14 }
15 }
16 return true;
17 }
18 };
LeetCode OJ:Valid Sudoku(有效数独问题)的更多相关文章
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- [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验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- LeetCode(38)-Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- [leetcode] 20. Valid Sudoku
这道题目被放在的简单的类别里是有原因的,题目如下: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode 036 Valid Sudoku
题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...
随机推荐
- NIO 03
1. 客户端要主动去连接:channel.connect(new InetSocketAddress("localhost",8888)); //用channel.finishCo ...
- 20145312 实验四《Andoid开发基础》
20145312 实验四<Andoid开发基础> 实验内容 1. 安装Android Studio 2. 运行安卓AVD模拟器 3. 使用Android运行出模拟手机并显示自己的学号 实验 ...
- libcstl中的list没法插入自定义数据
一开始运行出错,开启debug以后发现在push自定义对象的时候调试器提示找不到一个叫/XXX/XXXX/XXXX/libcstl-2.3.0/src/cstl_list_private.c</ ...
- Redis中RedisTemplate和Redisson管道的使用
当对Redis进行高频次的命令发送时,由于网络IO的原因,会耗去大量的时间.所以Redis提供了管道技术,就是将命令一次性批量的发送给Redis,从而减少IO. 一.Jedis对redis的管道进行操 ...
- 爬虫框架Scrapy之Downloader Middlewares
反反爬虫相关机制 Some websites implement certain measures to prevent bots from crawling them, with varying d ...
- spark SQL学习(认识spark SQL)
spark SQL初步认识 spark SQL是spark的一个模块,主要用于进行结构化数据的处理.它提供的最核心的编程抽象就是DataFrame. DataFrame:它可以根据很多源进行构建,包括 ...
- Matlab 日期频次统计
一.孕妇建档月份频次统计 源数据样本,为某医院一段时间内的孕妇建档时间 2015-04-22 10:12:522014-11-21 17:16:472013-12-16 17:35:442013-12 ...
- UOJ34 多项式乘法(NTT)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 获取用户真实IP,php实现
function get_client_ip() { if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv(" ...
- Memcached stats sizes 命令
Memcached stats sizes 命令用于显示所有item的大小和个数. 该信息返回两列,第一列是 item 的大小,第二列是 item 的个数. 语法: stats sizes 命令的基本 ...