diff --git a/1-100q/12.py b/1-100q/12.py new file mode 100644 index 0000000..d1208e8 --- /dev/null +++ b/1-100q/12.py @@ -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 + \ No newline at end of file diff --git a/1-100q/13.py b/1-100q/13.py new file mode 100644 index 0000000..aafd3a4 --- /dev/null +++ b/1-100q/13.py @@ -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 \ No newline at end of file diff --git a/1-100q/20.py b/1-100q/20.py new file mode 100644 index 0000000..21797ae --- /dev/null +++ b/1-100q/20.py @@ -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 \ No newline at end of file diff --git a/1-100q/27.py b/1-100q/27.py new file mode 100644 index 0000000..b7ae672 --- /dev/null +++ b/1-100q/27.py @@ -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 \ No newline at end of file diff --git a/1-100q/37.py b/1-100q/37.py new file mode 100644 index 0000000..43a2d9d --- /dev/null +++ b/1-100q/37.py @@ -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 + + \ No newline at end of file diff --git a/1-100q/43.py b/1-100q/43.py new file mode 100644 index 0000000..6930653 --- /dev/null +++ b/1-100q/43.py @@ -0,0 +1,3 @@ +class Solution: + def multiply(self, num1: str, num2: str) -> str: + return str(int(num1) * int(num2)) \ No newline at end of file diff --git a/1-100q/51.py b/1-100q/51.py new file mode 100644 index 0000000..ce32c43 --- /dev/null +++ b/1-100q/51.py @@ -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 + \ No newline at end of file diff --git a/1-100q/52.py b/1-100q/52.py new file mode 100644 index 0000000..2bf6fc6 --- /dev/null +++ b/1-100q/52.py @@ -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 \ No newline at end of file diff --git a/1-100q/7.py b/1-100q/7.py new file mode 100644 index 0000000..26244c7 --- /dev/null +++ b/1-100q/7.py @@ -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 \ No newline at end of file diff --git a/1-100q/88.py b/1-100q/88.py new file mode 100644 index 0000000..4828ce6 --- /dev/null +++ b/1-100q/88.py @@ -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)