LeetCode之“散列表”: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.
这道题解法暂没有比较巧妙的,所以下边程序所用方法就是Brute Force(暴力破解):
- class Solution {
- public:
- int charToInt(char c)
- {
- char str[] = {'', '', '', '', '', '', '', '', ''};
- int intStr[] = {, , , , , , , , };
- for(int i = ; i < ; i++)
- {
- if(c == str[i])
- return intStr[i];
- }
- return -;
- }
- bool isValidSudoku(vector<vector<char>>& board) {
- unordered_map<int, int> hashMap;
- for(int i = ; i < ; i++)
- hashMap[i] = ;
- // row by row
- for(int i = ; i < ; i++)
- {
- for(int k = ; k < ; k++)
- hashMap[k] = ;
- for(int j = ; j < ; j++)
- {
- int tmp = charToInt(board[i][j]);
- if(tmp != -)
- {
- hashMap[tmp]++;
- if(hashMap[tmp] > )
- return false;
- }
- }
- }
- // column by column
- for(int j = ; j < ; j++)
- {
- for(int k = ; k < ; k++)
- hashMap[k] = ;
- for(int i = ; i < ; i++)
- {
- int tmp = charToInt(board[i][j]);
- if(tmp != -)
- {
- hashMap[tmp]++;
- if(hashMap[tmp] > )
- return false;
- }
- }
- }
- // 3*3 boxes by 3*3 boxes
- for(int i = ; i < ; i += )
- {
- for(int j = ; j < ; j += )
- {
- for(int k = ; k < ; k++)
- hashMap[k] = ;
- for(int m = i; m < i + ; m++)
- for(int n = j; n < j + ; n++)
- {
- int tmp = charToInt(board[m][n]);
- if(tmp != -)
- {
- hashMap[tmp]++;
- if(hashMap[tmp] > )
- return false;
- }
- }
- }
- }
- return true;
- }
- };
LeetCode之“散列表”:Valid Sudoku的更多相关文章
- leetcode第35题--Valid Sudoku
题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【leetcode❤python】 36. Valid Sudoku
#-*- coding: UTF-8 -*-#特定的九个格内1-9的个数至多为1#依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.class Solutio ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- LeetCode之“散列表”:Isomorphic Strings
题目链接 题目要求: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic i ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- leetcode个人题解——#36 valid Sudoku
思路题目里已经给出来了,判断是否是一个有效数独,只需满足以下三个条件: 1.同行元素不重复且1-9都有: 2.同列元素不重复且1-9都有: 3.每个粗线分隔的3*3的小九宫格元素不重复且1-9都有. ...
- leetcode@ [36/37] Valid Sudoku / Sudoku Solver
https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...
随机推荐
- 一个数组保存了N个结构,每个结构保存了一个坐标,结构间的坐标都不相同,请问如何找到指定坐标的结构(除了遍历整个数组,是否有更好的办法)?
#include <iostream> #include <map> using namespace std; #define N 5 typedef struct point ...
- Dynamics CRM2013/2015 检索实体属性的两种方式
昨天有朋友问起如何查询一个字段属性是否存在于某个实体中,一般这个问题我们会采取最直观的查询方式即MetadataBrowser,该工具是一个zip解决方案包在SDK中的如下目录内"\SDK\ ...
- Microsoft Dynamics CRM 2011 当您在 大型数据集上执行 RetrieveMultiple 查询很慢的解决方法
症状 当您在 Microsoft Dynamics CRM 2011 年大型数据集上执行 RetrieveMultiple 查询时,您会比较慢. 原因 发生此问题是因为大型数据集缓存 Retrieve ...
- Java中导出到Excel实现_aspose.cells
参考http://183615215-qq-com.iteye.com/blog/1858208 包下载:http://pan.baidu.com/s/1o6ju0ZK,将lib的jar包导入到工程中 ...
- UNIX环境高级编程——单实例的守护进程
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...
- Python与JavaWeb的第一次碰撞
在Python中向服务器提交一个表单数据看起来是很容易的,但是这次经历着实让我记忆深刻,借此也为了警醒同样遇到了这样问题的你们. 要做什么? 使用Python的urllib2模块提交表单数据,并在服务 ...
- UNIX环境高级编程——线程属性之分离属性
说到线程的分离状态,我认为,之所以会有这个状态,是因为系统对某些线程的终止状态根本不感兴趣导致的. 我们知道,进程中的线程可以调用: int pthread_join(pthread_t tid, v ...
- UNIX环境高级编程——epoll函数使用详解
epoll - I/O event notification facility 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是 ...
- PHP解决中文乱码问题
初学PHP,在汉字页面间传输和转换的时候,遇到了中文乱码问题. 究其原因乱码无外乎以下几种情况: 1.html页本身的乱码问题, 解决方法:纯html页使用<meta http-equiv=&q ...
- ios第三方数据请求 UI_15
AppDelegate.m //指定根视图 self.window.rootViewController = [[[UINavigationController alloc]initWithRootV ...