Compare commits
3 Commits
749f7846a5
...
3f5146d5a8
Author | SHA1 | Date | |
---|---|---|---|
3f5146d5a8 | |||
fb0684dd27 | |||
5e3a66aa45 |
@ -1,21 +1,24 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def letterCombinations(self, digits: str) -> List[str]:
|
||||
if len(digits) == 0:
|
||||
return []
|
||||
|
||||
options = {
|
||||
'2': ('a', 'b', 'c'),
|
||||
'3': ('d', 'e', 'f'),
|
||||
'4': ('g', 'h', 'i'),
|
||||
'5': ('j', 'k', 'l'),
|
||||
'6': ('m', 'n', 'o'),
|
||||
'7': ('p', 'q', 'r', 's'),
|
||||
'8': ('t', 'u', 'v'),
|
||||
'9': ('w', 'x', 'y', 'z')
|
||||
"2": ("a", "b", "c"),
|
||||
"3": ("d", "e", "f"),
|
||||
"4": ("g", "h", "i"),
|
||||
"5": ("j", "k", "l"),
|
||||
"6": ("m", "n", "o"),
|
||||
"7": ("p", "q", "r", "s"),
|
||||
"8": ("t", "u", "v"),
|
||||
"9": ("w", "x", "y", "z"),
|
||||
}
|
||||
|
||||
pools = (options[digit] for digit in digits)
|
||||
result = [""]
|
||||
for pool in pools:
|
||||
result = [x+y for x in result for y in pool]
|
||||
result = [x + y for x in result for y in pool]
|
||||
return result
|
||||
|
50
leetcode/36_valid_sudoku.py
Normal file
50
leetcode/36_valid_sudoku.py
Normal file
@ -0,0 +1,50 @@
|
||||
from typing import List
|
||||
import pytest
|
||||
|
||||
|
||||
class Solution:
|
||||
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
||||
rows, cols, cubes = set(), set(), set()
|
||||
for i in range(9):
|
||||
for j in range(9):
|
||||
val = board[i][j]
|
||||
if not val.isdigit():
|
||||
continue
|
||||
|
||||
cube = (i // 3) * 3 + (j // 3)
|
||||
if (i, val) in rows or (j, val) in cols or (cube, val) in cubes:
|
||||
return False
|
||||
|
||||
rows.add((i, val))
|
||||
cols.add((j, val))
|
||||
cubes.add((cube, val))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"sudoku, expected",
|
||||
[
|
||||
(
|
||||
[
|
||||
["5", "3", ".", ".", "7", ".", ".", ".", "."],
|
||||
["6", ".", ".", "1", "9", "5", ".", ".", "."],
|
||||
[".", "9", "8", ".", ".", ".", ".", "6", "."],
|
||||
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
|
||||
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
|
||||
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
|
||||
[".", "6", ".", ".", ".", ".", "2", "8", "."],
|
||||
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
|
||||
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
|
||||
],
|
||||
True,
|
||||
)
|
||||
],
|
||||
)
|
||||
def test_search(solution: Solution, sudoku: List[List[str]], expected: bool):
|
||||
assert solution.isValidSudoku(sudoku) == expected
|
@ -1,3 +1,4 @@
|
||||
from typing import List
|
||||
import pytest
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user