题目链接

  题目要求:

  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(暴力破解):

  1. class Solution {
  2. public:
  3. int charToInt(char c)
  4. {
  5. char str[] = {'', '', '', '', '', '', '', '', ''};
  6. int intStr[] = {, , , , , , , , };
  7. for(int i = ; i < ; i++)
  8. {
  9. if(c == str[i])
  10. return intStr[i];
  11. }
  12. return -;
  13. }
  14.  
  15. bool isValidSudoku(vector<vector<char>>& board) {
  16. unordered_map<int, int> hashMap;
  17. for(int i = ; i < ; i++)
  18. hashMap[i] = ;
  19. // row by row
  20. for(int i = ; i < ; i++)
  21. {
  22. for(int k = ; k < ; k++)
  23. hashMap[k] = ;
  24. for(int j = ; j < ; j++)
  25. {
  26. int tmp = charToInt(board[i][j]);
  27. if(tmp != -)
  28. {
  29. hashMap[tmp]++;
  30. if(hashMap[tmp] > )
  31. return false;
  32. }
  33. }
  34. }
  35. // column by column
  36. for(int j = ; j < ; j++)
  37. {
  38. for(int k = ; k < ; k++)
  39. hashMap[k] = ;
  40. for(int i = ; i < ; i++)
  41. {
  42. int tmp = charToInt(board[i][j]);
  43. if(tmp != -)
  44. {
  45. hashMap[tmp]++;
  46. if(hashMap[tmp] > )
  47. return false;
  48. }
  49. }
  50. }
  51. // 3*3 boxes by 3*3 boxes
  52. for(int i = ; i < ; i += )
  53. {
  54. for(int j = ; j < ; j += )
  55. {
  56. for(int k = ; k < ; k++)
  57. hashMap[k] = ;
  58. for(int m = i; m < i + ; m++)
  59. for(int n = j; n < j + ; n++)
  60. {
  61. int tmp = charToInt(board[m][n]);
  62. if(tmp != -)
  63. {
  64. hashMap[tmp]++;
  65. if(hashMap[tmp] > )
  66. return false;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. return true;
  73. }
  74. };

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. 一个数组保存了N个结构,每个结构保存了一个坐标,结构间的坐标都不相同,请问如何找到指定坐标的结构(除了遍历整个数组,是否有更好的办法)?

    #include <iostream> #include <map> using namespace std; #define N 5 typedef struct point ...

  2. Dynamics CRM2013/2015 检索实体属性的两种方式

    昨天有朋友问起如何查询一个字段属性是否存在于某个实体中,一般这个问题我们会采取最直观的查询方式即MetadataBrowser,该工具是一个zip解决方案包在SDK中的如下目录内"\SDK\ ...

  3. Microsoft Dynamics CRM 2011 当您在 大型数据集上执行 RetrieveMultiple 查询很慢的解决方法

    症状 当您在 Microsoft Dynamics CRM 2011 年大型数据集上执行 RetrieveMultiple 查询时,您会比较慢. 原因 发生此问题是因为大型数据集缓存 Retrieve ...

  4. Java中导出到Excel实现_aspose.cells

    参考http://183615215-qq-com.iteye.com/blog/1858208 包下载:http://pan.baidu.com/s/1o6ju0ZK,将lib的jar包导入到工程中 ...

  5. UNIX环境高级编程——单实例的守护进程

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...

  6. Python与JavaWeb的第一次碰撞

    在Python中向服务器提交一个表单数据看起来是很容易的,但是这次经历着实让我记忆深刻,借此也为了警醒同样遇到了这样问题的你们. 要做什么? 使用Python的urllib2模块提交表单数据,并在服务 ...

  7. UNIX环境高级编程——线程属性之分离属性

    说到线程的分离状态,我认为,之所以会有这个状态,是因为系统对某些线程的终止状态根本不感兴趣导致的. 我们知道,进程中的线程可以调用: int pthread_join(pthread_t tid, v ...

  8. UNIX环境高级编程——epoll函数使用详解

    epoll - I/O event notification facility 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是 ...

  9. PHP解决中文乱码问题

    初学PHP,在汉字页面间传输和转换的时候,遇到了中文乱码问题. 究其原因乱码无外乎以下几种情况: 1.html页本身的乱码问题, 解决方法:纯html页使用<meta http-equiv=&q ...

  10. ios第三方数据请求 UI_15

    AppDelegate.m //指定根视图 self.window.rootViewController = [[[UINavigationController alloc]initWithRootV ...