Skip to content
Open
25 changes: 25 additions & 0 deletions 1-100q/12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 1 -> "I"
# 2 -> "II"
# 3 -> "III"
# 4 -> "IV"
# 5 -> "V"
# 1499 -> "MCDXCIX"


class Solution:
def intToRoman(self, num: int) -> str:
nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
letters = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
result = ""
i = 0

while num > 0:
times = num // nums[i]

result += letters[i] * times
num -= nums[i] * times

i += 1

return result

28 changes: 28 additions & 0 deletions 1-100q/13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def romanToInt(self, s: str) -> int:
letter = {
"I": 1,
"IV": 4,
"V": 5,
"IX": 9,
"X": 10,
"XL": 40,
"L": 50,
"XC": 90,
"C": 100,
"CD": 400,
"D": 500,
"CM": 900,
"M": 1000,
}
result = 0

while s:
if s[0:2] in letter:
result += letter.get(s[0:2])
s = s[2:]
else:
result += letter.get(s[0])
s = s[1:]

return result
18 changes: 18 additions & 0 deletions 1-100q/20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def isValid(self, s: str) -> bool:
parentheses = {
"(": ")",
"{": "}",
"[": "]",
}
stack = []

for char in s:
if char in parentheses:
stack.append(char)
elif stack and parentheses[stack[-1]] == char:
stack.pop()
else:
return False

return not stack
16 changes: 16 additions & 0 deletions 1-100q/27.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def removeElement(self, nums, val):
ret = 0
i = 0

while i < len(nums):
if nums[i] == val:
nums.remove(nums[i])
nums.append(None)
elif nums[i] == None:
i += 1
else:
i += 1
ret += 1

return ret
49 changes: 49 additions & 0 deletions 1-100q/37.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution:
def solveSudoku(self, bo):
"""
Do not return anything, modify board in-place instead.
"""
find = self.find_empty(bo)

if not find:
return True # solution found

row, col = find

for num in range(1, 10):
if self.is_valid(bo, row, col, num):
bo[row][col] = str(num)

if self.solveSudoku(bo):
return True

bo[row][col] = "."

return False

def find_empty(self, bo):
for x in range(len(bo)):
for y in range(len(bo[x])):
if bo[x][y] == ".":
return (x, y)

def is_valid(self, bo, row, col, val):
val = str(val)
# check row and col
for x in range(len(bo)):
for y in range(len(bo[x])):
if bo[x][col] == val and x != row or bo[row][y] == val and y != col:
return False

# check 3x3 box
box_x = row // 3
box_y = col // 3

for x in range(box_x * 3, box_x * 3 + 3):
for y in range(box_y * 3, box_y * 3 + 3):
if bo[x][y] == val and (x, y) != (row, col):
return False

return True


3 changes: 3 additions & 0 deletions 1-100q/43.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def multiply(self, num1: str, num2: str) -> str:
return str(int(num1) * int(num2))
46 changes: 46 additions & 0 deletions 1-100q/51.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution:
def solveNQueens(self, n: int):
solutions = []
state = []
self.search(state, solutions, n)
return solutions

def search(self, state, solutions, n):
if len(state) == n: # one more solution found
sol_str = self.to_string(state, n)
solutions.append(sol_str)
return

for candidate in self.possible_candidates(state, n):
# recursion
state.append(candidate)
self.search(state, solutions, n)
state.pop()

def possible_candidates(self, state, n):
if not state:
return range(n)

position = len(state)
candidates = set(range(n))

for row, col in enumerate(state):
# check row and col
candidates.discard(col)
# check diagonal
diagonal = position - row

candidates.discard(col + diagonal)
candidates.discard(col - diagonal)

return candidates

def to_string(self, state, n):
# [1, 3, 0, 2] -> [".Q..", "...Q", "Q...", "..Q."]
result = []

for i in state:
result.append("." * i + "Q" + "." * (n - i - 1))

return result

34 changes: 34 additions & 0 deletions 1-100q/52.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution:
def totalNQueens(self, n: int) -> int:
solutions = []
state = []
self.search(solutions, state, n)
return len(solutions)

def search(self, solutions, state, n):
if len(state) == n:
solutions.append(state)
return

for candidate in self.possible_candidates(state, n):
state.append(candidate)
self.search(solutions, state, n)
state.pop()

def possible_candidates(self, state, n):
if not state:
return range(n)

position = len(state)
candidates = set(range(n))

for row, col in enumerate(state):
# check row and col
candidates.discard(col)

#check diaconal
dist = position - row
candidates.discard(col - dist)
candidates.discard(col + dist)

return candidates
11 changes: 11 additions & 0 deletions 1-100q/7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def reverse(self, x: int) -> int:
revx = int(str(x)[::-1].replace("-", ""))

if x < 0:
revx *= -1

if revx >= 2**31-1 or revx <= -2**31:
return 0

return revx
17 changes: 17 additions & 0 deletions 1-100q/88.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def merge(self, nums1, m, nums2, n):
for i in range(len(nums1)):
if i >= m:
nums1.pop()

i = 0

while nums2 and i < len(nums1):
if nums2[0] <= nums1[i]:
nums1.insert(i, nums2[0])
nums2.pop(0)
else:
i += 1

if nums2:
nums1.extend(nums2)