题目链接

  题目要求:

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

  1. leetcode第35题--Valid Sudoku

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

  2. LeetCode(36)Valid Sudoku

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

  3. 【leetcode❤python】 36. Valid Sudoku

    #-*- coding: UTF-8 -*-#特定的九个格内1-9的个数至多为1#依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.class Solutio ...

  4. 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 ...

  5. LeetCode之“散列表”:Isomorphic Strings

    题目链接 题目要求: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic i ...

  6. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  7. LeetCode之“散列表”:Single Number

    题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...

  8. leetcode个人题解——#36 valid Sudoku

    思路题目里已经给出来了,判断是否是一个有效数独,只需满足以下三个条件: 1.同行元素不重复且1-9都有: 2.同列元素不重复且1-9都有: 3.每个粗线分隔的3*3的小九宫格元素不重复且1-9都有. ...

  9. leetcode@ [36/37] Valid Sudoku / Sudoku Solver

    https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...

随机推荐

  1. Android fragment(片段)构建灵活的UI

    在以支持多种屏幕尺寸为目标设计应用时,您可以在不同的布局配置中重复使用您的fragment 从而根据可用的屏幕空间优化用户体验. 例如,在手机设备上,由于采用单窗格用户界面,因此可能更适合一次只显示一 ...

  2. JBOSS EAP 6 系列五 Managed domains 管理域最主要的功能是“统一部署,统一配置”

    摘要 本文首先介绍Managed Domain的概念,管理域最主要的功能是"统一部署,统一配置".接下来通过一个实例在"统一配置"部分实现一个双机配置起来的域, ...

  3. python地理数据处理库geopy

    http://blog.csdn.net/pipisorry/article/details/52205266 python地理位置处理 python地理编码地址以及用来处理经纬度的库 GeoDjan ...

  4. java中抽象类的定义和使用

    java虽然比较简单,但是细节的知识点还是很多的,现在,介绍一下抽象类的定义和实现基础. 指的是在类中定义方法,而不去实现它,而在它的子类中去具体实现,继承抽象类的子类必须实现父类的抽象方法,除非子类 ...

  5. 6.0、Android Studio性能优化工具

    显示图像包含四个步骤.简单来说,CPU对比显示列表,GPU渲染图片显示,内存存储图片和数据,电池提供点力能源.每个部分的硬件都有限制,超过这个限制会导致应用运行较慢,显示性能差,或者耗电. 为了查找造 ...

  6. 【编程练习】poj1068

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24202   Accepted: 14201 De ...

  7. MySQL设计软件登录模块

    学了一段时间的Java了,思量着做一点简单的小模块的东西吧,于是就有了下面的这个简单的小案例. 大致实现的功能就是注册于登录还有就是用到了一点,分层思想.仅此而已,所以非常的适合新手围观. 建立好数据 ...

  8. iOS 南京互联网大会分享及个人见解 韩俊强的博客

    首先分两大块: 1.如何打造高效/稳定的App (重点): 2.软件自动化测试: 每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 每日更新关注:http://we ...

  9. 精通CSS+DIV网页样式与布局--设置表单和表格

    表格和表单是网页中非常重要的两个元素,在上篇博客中,我们简单的介绍了CSS的页面背景设置,今天小编继续来介绍CSS的相关知识,在我们的CSS中如何设置表格和表单,首先,来看一张思维导图,通过图简单的预 ...

  10. ADO.NET常用方法释义

    先列个列表,下面的就是常用的数据库操作的方法. ExecuteNonQuery 释义:对链接执行的SQL语句,并返回受影响的行数(注意:用它来执行目录操作,如查询数据库的结构,创建表等数据库对象,或通 ...