判断数独是否合法-LintCode
来源:互联网 发布:成都安吉斯主机编程 编辑:程序博客网 时间:2024/05/17 02:09
请判定一个数独是否有效。
该数独可能只填充了部分数字,其中缺少的数字用 . 表示。
注意事项:
一个合法的数独(仅部分填充)并不一定是可解的。我们仅需使填充的空格有效即可。
说明:
什么是 数独?
http://sudoku.com.au/TheRules.aspx
http://baike.baidu.com/subview/961/10842669.htm
样例:
The following partially filed sudoku is valid.
#ifndef C389_H#define C389_H#include<iostream>#include<vector>#include<map>using namespace std;class Solution {public: /* * @param board: the board * @return: whether the Sudoku is valid */ bool isValidSudoku(vector<vector<char>> &board) { // write your code here int m = board.size(); int n = board[0].size(); map<char, int> count; for (int i = 0; i < m; ++i) { count.clear(); for (int j = 0; j < n; ++j) { if (isdigit(board[i][j])) { if (count.find(board[i][j]) == count.end()) { count[board[i][j]]++; } else { return false; break; } } } } for (int j = 0; j < n; ++j) { count.clear(); for (int i = 0; i < m; ++i) { if (isdigit(board[i][j])) { if (count.find(board[i][j]) == count.end()) { count[board[i][j]]++; } else { return false; break; } } } } vector<vector<int>> dir{ { 0, 0 }, { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 } }; vector<vector<int>> center{ { 1, 1 }, { 1, 4 }, { 1, 7 }, { 4, 1 }, { 4, 4 }, { 4, 7 }, { 7, 1 }, { 7, 4 }, { 7, 7 } }; for (auto c : center) { count.clear(); for (auto t : dir) { int x = c[0] + t[0]; int y = c[1] + t[1]; if (isdigit(board[x][y])) { if (count.find(board[x][y]) == count.end()) { count[board[x][y]]++; } else { return false; break; } } } } return true; }};#endif
阅读全文
0 0
- LintCode-判断数独是否合法
- LintCode:判断数独是否合法
- 判断数独是否合法-LintCode
- LintCode Valid Sudoku 判断数独是否合法
- [LintCode 389] 判断数独是否合法(Python)
- 判断数独是否合法
- 题目:判断数独是否合法
- 容易 判断数独是否合法
- LintCode398:判断数独是否合法
- leetcode 判断数独是否合法
- 36.判断数独是否合法
- LintCode算法题解——判断数独是否合法、平面列表、克隆二叉树、序列排号
- 【LintCode】判断数独合法
- ACdream 1195 判断数独棋盘是否合法
- 389.Valid Sudoku-判断数独是否合法(容易题)
- Leetcode:36.Valid Sudoku 初始化数独数组是否合法判断
- poj 3435 检测数独状态是否合法
- 6.15判断数独
- Python网络爬虫与信息提取(一)
- 3242: [Noi2013]快餐店
- uva 225 Golygons
- 和研究生博士学长聊天有感
- docker容器 最简单的java演示实例
- 判断数独是否合法-LintCode
- C++继承[详细]
- 文件管理,系统管理,系统安全常用指令整理
- 数据结构——图的广度优先搜索模板
- some tips about python One
- vue 计算属性 底部导航处理
- 影响mysql性能因素硬件、操作系统
- 数据结构-栈的基本操作
- Android -- 系统start Service流程简要分析