指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


036. Valid Sudoku (Easy)

链接

题目:https://leetcode.com/problems/valid-sudoku/

代码(github):https://github.com/illuz/leetcode

题意

推断一个数独是否有效。

有效的数独不强求有解。

分析

仅仅要同一行、列、块里没有同样数字即可了。

开个数组记录即可了。没什么难度。能够用二进制来表示。表位运算加速。

(注意是推断有效,不是有解,我刚開始给求解了,TLE 了好多次。

。)

代码

C++:

class Solution {
private:
int row[9], col[9], sqr[3][3];
bool check(int x, int y, int val) {
return !((row[x]>>val)&1) && !((col[y]>>val)&1) && !((sqr[x/3][y/3]>>val)&1);
}
void mark(int x, int y, int val) {
row[x] |= (1<<val);
col[y] |= (1<<val);
sqr[x/3][y/3] |= (1<<val);
}<pre name="code" class="java">// 求解 Sudoku
//	void unmark(int x, int y, int val) {
// row[x] -= (1<<val);
// col[y] -= (1<<val);
// sqr[x/3][y/3] -= (1<<val);
// }
// bool dfs(int pos, vector<vector<char> > &board) {
// // x = pos / 9, y = pos % 9
// if (pos == 81)
// return true;
// if (board[pos/9][pos%9] != '.') {
// return dfs(pos + 1, board);
// } else {
// for (int i = 0; i < 9; i++)
// if (check(pos/9, pos%9, i)) {
// mark(pos/9, pos%9, i);
// if (dfs(pos + 1, board))
// return true;
// unmark(pos/9, pos%9, i);
// }
// }
// return false;
// }
public:
bool isValidSudoku(vector<vector<char> > &board) {
memset(row, 0, sizeof(row));
memset(col, 0, sizeof(col));
memset(sqr, 0, sizeof(sqr));
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[i].size(); j++)
if (board[i][j] != '.') {
if (!check(i, j, board[i][j] - '1'))
return false;
mark(i, j, board[i][j] - '1');
}
return true;
// return dfs(0, board);
}
};



版权声明:本文博主原创文章,博客,未经同意不得转载。

[LeetCode] 036. Valid Sudoku (Easy) (C++)的更多相关文章

  1. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

  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】Valid Sudoku (easy)

    题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...

  4. LeetCode:36. Valid Sudoku,数独是否有效

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

  5. 【LeetCode】036. Valid Sudoku

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

  6. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  7. LeetCode 36 Valid Sudoku

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

  8. 【leetcode】Valid Sudoku

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

  9. Java [leetcode 36]Valid Sudoku

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

随机推荐

  1. 802.11(wi-fi)的PHY层(编码与调制方法)

    版本概要: 802.11-2007是目前的基础版本,之前的过时版本不考虑. 2009是较新的版本,就是目前最普及的802.11n.(100Mb/s) 2012就是传说中的802.11ac,工作在5G, ...

  2. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  3. OpenCV原则解读HAAR+Adaboost

    因为人脸检测项目.用途OpenCV在旧分类中的训练效果.因此该检测方法中所使用的分类归纳.加上自己的一些理解.重印一些好文章记录. 文章http://www.61ic.com/Article/DaVi ...

  4. 重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类

    原文:重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类 [源码下载] 重新想象 Windows 8 Store Apps (32) - 加密 ...

  5. emeditor 配置教程

    1.众多的图形界面配置功能 通过查看EmEditor的安装目录,可以发现,EmEditor有几个配置文件,理论上应该可以通过修改配置文件来达到配置EmEditor的目 的.然而,打开配置文件一看,如果 ...

  6. HR筒子说:程序猿面试那点事(转)

    小屁孩曾经有过4年的招聘经验,期间见识了各种类型的程序猿:有大牛.有菜牛:有功成名就,有苦苦挣扎不知方向.等后来做了一枚程序猿之后发现,HR眼中的程序猿和程序猿中的HR都是不一样的.有感与此,从HR的 ...

  7. [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 55273   ...

  8. Ural 1309 Dispute (递归)

    意甲冠军: 给你一个数列: f(0) = 0 f(n) = g(n,f(n-1)) g(x,y) = ((y-1)*x^5+x^3-xy+3x+7y)%9973 让你求f(n)  n <= 1e ...

  9. Spring.net-业务层仓储

    Spring.net-业务层仓储 本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们 ...

  10. iOS 9 新特性

    这篇文章介绍了iOS9开发相关的简介,现在发布的设备都会搭载iOS9.这篇文章也列出了详细描述新特性的文章. iPad多线程增强 iOS9使用Slider Over, Split View, Pict ...