LeetCode 036 Valid Sudoku

时间:2022-09-16 15:56:45

题目要求: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 '.'.

LeetCode 036 Valid Sudoku

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.

分析:

本题是让写数独的check函数(isValidSudoku)~基本有三个步骤:

① 每一行都合法;

② 每一列都合法;

③ 每一块都合法(3 * 3)。

代码如下:

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return
isValidRow(board) && isValidColumn(board) && isValidBox(board);
} private: bool isValidRow(vector<vector<char> > &board) {
int count[9];
for (int i = 0; i < 9; i++) {
memset(count, 0, sizeof(int) * 9);
for (int j = 0; j < 9; j++) {
if (!add(count, board[i][j])) {
return false;
}
}
}
return true;
} bool isValidColumn(vector<vector<char> > &board) {
int count[9];
for (int i = 0; i < 9; i++) {
memset(count, 0, sizeof(int) * 9);
for (int j = 0; j < 9; j++) {
if (!add(count, board[j][i])) {
return false;
}
}
}
return true;
} bool isValidBox(vector<vector<char> > &board) {
int point[9][2] = {
{1, 1}, {1, 4}, {1, 7}, {4, 1}, {4, 4}, {4, 7}, {7, 1}, {7, 4}, {7, 7}
};
int dir[8][2] = {
{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}
};
int count[10]; for (int i = 0, x, y; i < 9; i++) {
memset(count, 0, sizeof(int) * 10);
x = point[i][0];
y = point[i][1];
add(count, board[x][y]);
for (int j = 0; j < 8; j++) {
if (!add(count, board[dir[j][0] + x][dir[j][1] + y])) {
return false;
}
}
}
return true;
} bool add(int count[], char c) {
if (c == '.') {
return true;
} //如果每个字符出现次数 <= 1,则正常+_+
return (++count[c - '1']) <= 1;
}
};

简化版本:

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
// Note: The Solution object is instantiated only once.
vector<vector<bool>> rows(9, vector<bool>(9,false));
vector<vector<bool>> cols(9, vector<bool>(9,false));
vector<vector<bool>> blocks(9, vector<bool>(9,false)); for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++){
if(board[i][j] == '.')continue; //rows, cols, blocks分别是9个布尔值
//将每个点分别赋值为true
//若未赋值的为true了,则有问题
int num = board[i][j] - '1';
if(rows[i][num] || cols[j][num] || blocks[i - i%3 + j/3][num])
return false;
rows[i][num] = cols[j][num] = blocks[i - i%3 + j/3][num] = true;
}
return true;
} };

LeetCode 036 Valid Sudoku的更多相关文章

  1. &lbrack;LeetCode&rsqb; 036&period; Valid Sudoku &lpar;Easy&rpar; &lpar;C&plus;&plus;&rpar;

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

  2. Java for LeetCode 036 Valid Sudoku

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

  3. LeetCode&colon;36&period; Valid Sudoku,数独是否有效

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

  4. 【LeetCode】036&period; Valid Sudoku

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

  5. LeetCode 36 Valid Sudoku

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

  6. 【leetcode】Valid Sudoku

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

  7. Java &lbrack;leetcode 36&rsqb;Valid Sudoku

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

  8. LeetCode&lpar;38&rpar;-Valid Sudoku

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

  9. &lbrack;leetcode&rsqb; 20&period; Valid Sudoku

    这道题目被放在的简单的类别里是有原因的,题目如下: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. ...

随机推荐

  1. &lbrack;已解决&rsqb; java&period;net&period;InetAddress&period;getHostName&lpar;&rpar; 阻塞问题

    在学习java nio的过程中发现某些情况下使用该方法会导致程序阻塞,(情况:服务器,Linux:客户端,WIN10) java.net.InetAddress.getHostName() 阻塞情况如 ...

  2. Git教程之创建版本库&lpar;2&rpar;

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...

  3. 微信朋友圈分享页面(JS-SDK 1&period;0)

    微信更新sdk后大量分享朋友圈代码失效,标题 缩略图 描述无法自定义 新版SDK分享文章步骤 1.绑定域名 (方法参考 http://mp.weixin.qq.com/wiki/7/aaa137b55 ...

  4. POPTEST老李谈Debug和Release的区别(c&num;)

    POPTEST老李谈Debug和Release的区别(c#)   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  5. alpha冲刺10&sol;10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之10(匆匆而过) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 答辩演练 版本演示视频拍摄 接下来的计划 ...

  6. apiCloud检出代码出现以下图示错误:

    问题如下: Initialized empty Git repository in H:/simlpe/.git/ 已经在 H:\simlpe 完成必要的项目初始化工作正在尝试从代码服务器获取数据.. ...

  7. logback节点配置详解

    一 :根节点  <configuration></configuration> 属性 : debug : 默认为false ,设置为true时,将打印出logback内部日志信 ...

  8. windows磁盘API实践

    API的列表如下,网上找的,我觉得还是蛮详细的: 磁盘和驱动器管理APIGetLogicalDrivers       获取主机中所有的逻辑驱动器,以BitMap的形式返回.GetLogicalDri ...

  9. coursera 《现代操作系统》 -- 第五周 同步机制(2)

    分清紧急等待队列与条件等待队列(c 链) 条件等待队列:但是进入管程的这个进程可能由于对资源的操作的过程中发现条件不成熟, 那么它就不能够继续对资源进行相应的操作. 我们以生产者. 消费者为例. 如果 ...

  10. sklearn实践&lowbar;普通线性回归

    import pandas as pd import numpy as np import matplotlib.pyplot as plt data = pd.read_csv(r"C:\ ...