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

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

LeetCode之“散列表”:Valid Sudoku的相关文章

LeetCode(36)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 (partia

leetcode第35题--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 (partial

【leetcode?python】 36. Valid Sudoku

数独规则如下:相当于一个9*9的矩阵 代码如下:#特定的九个格内1-9的个数至多为1#依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.class Solution(object):    def isValidSudoku(self, board):        """        :type board: List[List[str]]        :rtype: bool        """

LeetCode之“散列表”:Contains Duplicate &amp;&amp; Contains Duplicate II

 1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 代码如下:

LeetCode之“散列表”:Isomorphic Strings

题目链接 题目要求: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of c

LeetCode之“散列表”:Single Number

题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 这道题属于比较简单的,程序如下: 1 class Solutio

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ 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 sud

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行