【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 ...
随机推荐
- 使用Git向GitHub上上传代码
参考:http://www.cnblogs.com/yxhblogs/p/6130995.html 如果遇到[git无法pull仓库refusing to merge unrelated histor ...
- 洛谷 P2515 [HAOI2010]软件安装(缩点+树形dp)
题面 luogu 题解 缩点+树形dp 依赖关系可以看作有向边 因为有环,先缩点 缩点后,有可能图不联通. 我们可以新建一个结点连接每个联通块. 然后就是树形dp了 Code #include< ...
- Python 实现 ZoomEye API SDK
版权声明:未经作者授权,禁止转载! ZoomEye想必大家都很熟悉,自从官方开放了API,网上各种版本的SDK乱飞.今天我也来发一个自己写的. 首先我们从https://github.com/SEC0 ...
- 执行AJAX返回HTML片段中的JavaScript脚本
如果AJAX加载的数据是一个HTML片段,而且这个HTML片段还包含脚本<script>块,那么在你把这数据xmlHttp.responseText用innerHTML方法插入到当前文档一 ...
- Putty之public key ssh认证入门
1.工作平台 客户端:Win2kEn Sp3,Putty Beta 0.53 服务器:RedHat72,OpenSSH_3.4p1 2.Putty简介 一个免费小巧的Win32平台下的ssh客户端.它 ...
- phantomjs的和谷歌浏览器的简单使用
一.phantomjs的简单使用 ''' 什么是phantomJs:无界面的浏览器 ''' from selenium import webdriver from time import sleep ...
- 2019.3.22 SQL语句(基础篇)
SQL语句 创建一个数据库: create database+数据库名; 使用数据库: use+数据库名; 查看mySQL中有哪些数据库: show databases; 删除数据库 drop dat ...
- PIE SDK SFIM融合
1.算法功能简介 SFIM 融合方法全称为基于平滑滤波的亮度变换.基本原理是将高分辨率影像通过低通滤波抑制其高频空间信息保留低频信息,再将原高分辨率影像与通过低通滤波的高分辨率影像进行比值运算,以抵消 ...
- 关于工具类静态方法调用@Autowired注入的service类问题
@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...
- Iterator遍历 (遍历集合)
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...