【Leetcode】【Easy】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 '.'.
![]()
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.
解题:
将每一行、每一列、每一个九格中的数字分别输入一个大小为9的数组中,记录输入的数字是否重复
代码:
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
return isValidRow(board) && isValidColumn(board) && isValidBox(board);
}
bool isValidRow(vector<vector<char> > board) {
int count[];
for (int i = ; i < ; ++i) {
memset(count, , sizeof(int) * );
for (int j = ; j < ; ++j) {
if (!add(count, board[i][j]))
return false;
}
}
return true;
}
bool isValidColumn(vector<vector<char> > board) {
int count[];
for (int i = ; i < ; ++i) {
memset(count, , sizeof(int) * );
for (int j = ; j < ; ++j) {
if (!add(count, board[j][i]))
return false;
}
}
return true;
}
bool isValidBox(vector<vector<char> > board) {
int count[];
int point[][] = {
{, }, {, }, {, }, {, }, {, }, {, }, {, }, {, }, {, }
};
for (int i = ; i < ; ++i) {
memset(count, , sizeof(int) * );
for (int x = ; x < ; ++x) {
for (int y = ; y < ; ++y) {
int abscissa = point[i][] + x;
int ordinate = point[i][] + y;
if (!add(count, board[abscissa][ordinate]))
return false;
}
}
}
return true;
}
bool add(int count[], char dig) {
if (dig == '.')
return true;
else
return (++count[dig - '']) == ;
}
};
代码疑问:
1、为什么形参设置为board和&board都可以通过编译;
附录:
数独填充算法
【Leetcode】【Easy】Valid Sudoku的更多相关文章
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【leetcode刷题笔记】Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode之“散列表”:Valid Sudoku
题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 洛谷 P2515 [HAOI2010]软件安装(缩点+树形dp)
题面 luogu 题解 缩点+树形dp 依赖关系可以看作有向边 因为有环,先缩点 缩点后,有可能图不联通. 我们可以新建一个结点连接每个联通块. 然后就是树形dp了 Code #include< ...
- POJ - 1948 二维01背包
T了两发,DP方程很简单粗暴 dp[i][j][k]:用前i物品使得容量分别为j和k的背包恰好装满 背包的调用只需一次即可,第一次T就是每次check都丧心病狂地背包一次 对于sum的枚举,其实i j ...
- Oracle 设定字符集
在Redhat上安装Oracle 10g没有设定字符集,采用的是操作系统默认字符集:WE8ISO8859P1,将字符集修改为:ZHS16GBK.由于过程不可逆,首先需要备份数据库. 1.数据库全备 2 ...
- date +%F时间日期
date +%Y -%m-%d 年月日 date +%T 显示时间 HMS几点几分几秒 -%H 为小时 %w 周几 date -d “-1day” 一天之前 date ...
- oracle 查看一个表中的记录是否被锁住
SELECT a.object_id, a.session_id, b.object_nameFROM v$locked_object a, dba_objects bWHERE a.object_i ...
- jquery双日历日期选择器bootstrap-daterangepicker日历插件
这个插件既可以作为双日历也可以作为单日历插件(jquery的插件在jquery插件库中http://www.jq22.com/下载很方便,在CSDN下载真麻烦) 引用 <meta http-eq ...
- Activemq API使用(不整合spring)
首先需要引入activemq的jar包,这里用的是5.14.4版本的 <!-- https://mvnrepository.com/artifact/org.apache.activemq/ac ...
- 5. AQS(AbstractQueuedSynchronizer)抽象的队列式的同步器
5.1 AbstractQueuedSynchronizer里面的设计模式--模板模式 模板模式:父类定义好了算法的框架,第一步做什么第二步做什么,同时把某些步骤的实现延迟到子类去实现. 5.1.1 ...
- nodejs基础知识查缺补漏
1. 单线程.异步I/O.对比php nodejs是单线程的,但是是异步I/O,对于高并发时,它也能够快速的处理请求,100万个请求也可以承担,但是缺点是非常的耗内存,但是我们可以加大内存, 所以能用 ...
- Redis启动和关闭
带配置文件启动 ./redis-server redis.conf 关闭 无密码模式 ./redis-cli -h xxx -p xxx shutdown 密码模式 ./redis-cli -h ...