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 ...
随机推荐
- convention over configuration
惯例优先原则:也称为约定大于配置或规约大于配置(convention over configuration),即通过约定代码结构或命名规范来减少配置数量,同样不会减少配置文件:即通过约定好默认规范来提 ...
- jvm学习(重点)
http://blog.csdn.net/yfqnihao/article/details/8271665 http://blog.csdn.net/cutesource/article/detail ...
- [HAOI2017模拟]百步穿杨
今天的考试题. 考试的时候因为以前做过还写过题解,然后就以为模型已经很清楚了,然后就开始直接推.最后因为蜜汁自信一定能推出来,然后模型搞错了,只能交个暴力上去,于是这场考试GG. 第一次碰上这道题是在 ...
- bzoj 3450: Tyvj1952 Easy
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 411 Solved: 309[Submit][Status][Discuss] Descriptio ...
- jsp判断以某个字母开头
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ tag ...
- C#设计模式之控制反转即依赖注入-微软提供的Unity
使用VS2015的Nuget管理器下载Unity. 程序员接口类: 1 namespace UnityDemo 2 { 3 public interface IProgrammer 4 { 5 voi ...
- 一些常用的CDN列表
Microsoft Ajax Content Delivery Network 点击查看详细列表
- setInterva()调用
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭.由 s ...
- VMWare虚拟机网络配置
Bridged(桥接模式) 桥接模式相当于虚拟机和主机在同一个真实网段,VMWare充当一个集线器功能(一根网线连到主机相连的路由器上),所以如果电脑换了内网,静态分配的ip要更改.图如下: NAT( ...
- ionic+cordova 学习开发App(一)
一.项目所需环境 (一)jdk 1.jdk的安装,必须同时包含Java 和javac [一般安装包中都包含有,可以确定下] (二)node.js 和NPM 1.大多插件和辅助工具都运行在NPm平台上. ...