Skip to content

Commit dcd09db

Browse files
committed
update 36 Valid Sudoku
1 parent d9ba15e commit dcd09db

File tree

4 files changed

+75
-56
lines changed

4 files changed

+75
-56
lines changed

HashTable/36_ValidSudoku.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-13 21:09:57
4+
*/
5+
6+
class Solution
7+
{
8+
public:
9+
/*
10+
According to:
11+
https://discuss.leetcode.com/topic/8241/my-short-solution-by-c-o-n2
12+
*/
13+
bool isValidSudoku(vector<vector<char> > &board)
14+
{
15+
int row[9][9] = {0}, col[9][9] = {0}, panel[9][9] = {0};
16+
17+
for(int i = 0; i < board.size(); ++i)
18+
for(int j = 0; j < board[i].size(); ++j)
19+
if(board[i][j] != '.')
20+
{
21+
int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;
22+
if(row[i][num] || col[j][num] || panel[k][num])
23+
return false;
24+
row[i][num] = col[j][num] = panel[k][num] = 1;
25+
}
26+
27+
return true;
28+
}
29+
};
30+
31+
/*
32+
["..4...63.",".........","5......9.","...56....","4.3.....1","...7.....","...5.....",".........","........."]
33+
[".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]
34+
*/

HashTable/36_ValidSudoku.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def isValidSudoku(self, board):
7+
# check for rows
8+
for row in board:
9+
row_hash = {}
10+
for c in row:
11+
if c != "." and c in row_hash:
12+
return False
13+
row_hash[c] = 1
14+
15+
# check for cols
16+
for i in range(9):
17+
col_hash = {}
18+
for row in board:
19+
if row[i] != "." and row[i] in col_hash:
20+
return False
21+
col_hash[row[i]] = 1
22+
23+
# check for panel
24+
for i in range(0, 9, 3):
25+
for j in range(0, 9, 3):
26+
count = 0
27+
panel_hash = {}
28+
while(count < 9):
29+
c = board[i + count // 3][j + count % 3]
30+
count += 1
31+
if c != "." and c in panel_hash:
32+
return False
33+
panel_hash[c] = 1
34+
35+
return True
36+
37+
"""
38+
["..4...63.",".........","5......9.","...56....","4.3.....1","...7.....","...5.....",".........","........."]
39+
[".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]
40+
"""

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
* 001. [Two Sum](HashTable/01_TwoSum.py)
8989
* 003. [Longest Substring Without Repeating Characters](HashTable/03_LongestSubstringWithoutRepeatingCharacters.py)
9090
* 018. `4Sum`
91+
* 036. [Valid Sudoku](HashTable/36_ValidSudoku.py)
9192
* 049. [Group Anagrams](HashTable/49_GroupAnagrams.py)
9293
* 128. [Longest Consecutive Sequence](HashTable/128_LongestConsecutiveSequence.py)
9394
* 146. [LRU Cache](HashTable/146_LRUCache_pythonic.py)

Week05/36.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)