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的更多相关文章

  1. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【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 ...

  4. 【LeetCode题意分析&解答】36. Valid Sudoku

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

  5. 【leetcode刷题笔记】Valid Sudoku

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

  6. 【leetcode刷题笔记】Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  7. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  8. LeetCode之“散列表”:Valid Sudoku

    题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...

  9. 【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 ...

  10. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. hdu6183 Color it 线段树动态开点+查询减枝

    题目传送门 题目大意: 有多次操作.操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序. 思路: 由于查询的矩形 ...

  2. 2018南京icpc现场赛心得

    第一次参加icpc的比赛,也是第一块奖牌,虽然只是铜,但其实打的已经很好了,稍微差一点就可以摸银了. 之前参加省赛,成为那次比赛我校唯一一个没拿奖的队伍,其实还是一直都有一些心结的,而这段时间和新的队 ...

  3. UVA - 11795 状压DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  4. srping 事物管理

    1. 准备工作 1> 添加接口 BookShopDao package com.tx; public interface BookShopDao { //根据书号获取书的单价 public in ...

  5. C#串口编程 操作硬件

    -------通过USB将硬件连接PC, 更新USB驱动,使用下面方法可控制相关设备. //1声明一个串口对象 public static SerialPort MainModule_Port_ = ...

  6. MySQL数据库插入中文时出现Incorrect string value: '\xE6\x97\xB7\xE5\x85\xA8' for column 'sz_name' at row 1

    今天在开发时候出现了这个问题 Incorrect string value: '\xE6\x97\xB7\xE5\x85\xA8' for column 'sz_name' at row 1 场景,往 ...

  7. Thinkphp基于规则的Auth权限认证类

      PS:onethink是基于该权限认证类实现,Auth类作为官方类库,在Library\Think里面. 其实Auth类也是基于角色访问控制RBAC扩展的,具体到节点的权限校验方式还是需要根据业务 ...

  8. 昨天太晚了,今天教你用Debug模式来分析程序执行顺序

    还是以昨天的XML文件解析来做栗子,希望通过这个好吃的栗子可以举一反三 学会用debug来看源码和找Bug 事件类型主要有五种START_DOCUMENT:xml头的事件类型    = 0END_DO ...

  9. vim 配置文件——部分配置

    //vim 相关 set nu set showmatch set autoindent set smartindent set ruler set incsearch set tabstop=4 s ...

  10. bzoj 4543: [POI2014]Hotel加强版

    Description 给出一棵树求三元组 \((x,y,z)\,,x<y<z\) 满足三个点两两之间距离相等,求三元组的数量 Solution 考虑暴力 \(DP\) 设 \(f[i][ ...