题目链接

  题目要求:

  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. Servlet - 会话跟踪

    Servlet 标签 : Java与Web 会话跟踪 HTTP本身是"无状态"协议,它不保存连接交互信息,一次响应完成之后即连接断开,下一次请求需要重新建立连接,服务器不记录上次连 ...

  2. 悲观的并发策略——Synchronized互斥锁

    volatile既然不足以保证数据同步,那么就必须要引入锁来确保.互斥锁是最常见的同步手段,在并发过程中,当多条线程对同一个共享数据竞争时,它保证共享数据同一时刻只能被一条线程使用,其他线程只有等到锁 ...

  3. Java之继承深刻理解

    1.关于私有成员变量 无论父类中的成员变量是私有的.共有的.还是其它类型的,子类都会拥有父类中的这些成员变量.但是父类中的私有成员变量,无法在子类中直接访问,必须通过从父类中继承得到的protecte ...

  4. Java基本语法-----java注释

    1注释的作用 通过注释提高程序的可读性,是java程序的条理更加清晰,易于区分代码行与注释行.另外通常在程序开头加入作者,时间,版本,要实现的功能等内容注释,方便后来的维护以及程序员的交流. 2注释的 ...

  5. WINDOWS系统注册表取得管理权限研究

    有的时候开发我们需要取得系统管理员权限,可以通过修改注册表实现,研究网上的各种方法,整理得一下脚本实现取得管理员权限 脚本如下 Windows Registry Editor Version 5.00 ...

  6. Cocos2D中节点Z序的计算规则

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...

  7. 关于LT分发系统的设计构想

    git地址 https://github.com/cxyxd/LtDistribution 背景 对tomcat做集群,在多机多tomcat的情况下,如果要更新代码,只能手动的将代码复制,粘贴,然后下 ...

  8. SQL 数据库语言分析总结(二)

    介绍sql语言 我们接着一的顺序继续介绍这个语言 数据类型 整形: TINYINT(8位) SMALLINT(16位) MEDIUMINT(24位) INT(32位) BIGINT(64位) 实数: ...

  9. 最简单的视频编码器:基于libvpx(编码YUV为VP8)

    ===================================================== 最简单的视频编码器系列文章列表: 最简单的视频编码器:编译 最简单的视频编码器:基于libx ...

  10. Java正则表达式小记

    http://blog.csdn.net/pipisorry/article/details/51059500 正则表达式的一般规则都一样,见[python正则表达式] java正则表达式中的特殊字符 ...