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 ...
随机推荐
- OpenCV+OpenGL 双目立体视觉三维重建
0.绪论 这篇文章主要为了研究双目立体视觉的最终目标--三维重建,系统的介绍了三维重建的整体步骤.双目立体视觉的整体流程包括:图像获取,摄像机标定,特征提取(稠密匹配中这一步可以省略),立体匹配,三维 ...
- ERROR: In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing
eclipse sdk从低版本切换到高版本sdk的时候 v7包会包这个错ERROR: In <declare-styleable> MenuView, unable to find ...
- 深入浅出如何解析xml文件---下篇
在上篇博文中,小编主要介绍xml的两种解析方式,分别是dom4j和dom,今天这篇博文,小编主要来简单介绍一下xml的其她两种解析方式sax和jdom. sax解析xml文件 sax,全称是Simp ...
- SpriteKit物理引擎碰撞中5个重要信息
我们知道在SpriteKit物理引擎实际是基于Box2D! 在SpriteKit中当你设置好适当的碰撞参数后,通过遵守SKPhysicsContactDelegate,你可以选择实现2各碰撞回调方法: ...
- 悲观的并发策略——Synchronized互斥锁
volatile既然不足以保证数据同步,那么就必须要引入锁来确保.互斥锁是最常见的同步手段,在并发过程中,当多条线程对同一个共享数据竞争时,它保证共享数据同一时刻只能被一条线程使用,其他线程只有等到锁 ...
- 复习java逻辑---实现猜数字游戏
package shopping; import java.util.*; public class SendGiftMenu { public static void main(String[] a ...
- socket系列之服务器端socket——ServerSocket类
一般地,Socket可分为TCP套接字和UDP套接字,再进一步,还可以被分为服务器端套接字跟客户端套接字.这节我们先关注TCP套接字的服务器端socket,Java中ServerSocket类与之相对 ...
- 详解EBS接口开发之采购接收&退货处理之关键API--(补充)
PROCEDURE process_rcv_online(p_api_version IN NUMBER, p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_ ...
- 深入浅出EF之ModelFirst和DBFirst
在上篇博文中,小编主要简单的介绍了一下EF的一些基础知识,其中,小编蜻蜓点水的提了一下ModelFirst和DBFirst,ModelFirst先设计实体,然后根据模型生成数据库,DBFirst根据数 ...
- Cocos2D中使用CCDrawNode绘制几何图形崩溃的解决
在cocos2D v3.x中已经不能像在v2.x中那样直接调用ccDrawXXX函数来绘制几何图形了. 我们可以使用CCDrawNode或者CCRenderer来绘制图形. 但是官方的Api手册中说的 ...