diff --git a/reverse-bits/jinah92.py b/reverse-bits/jinah92.py new file mode 100644 index 000000000..4d2d8cf5e --- /dev/null +++ b/reverse-bits/jinah92.py @@ -0,0 +1,14 @@ +# O(1) time, O(1) space +class Solution: + def reverseBits(self, n: int) -> int: + stack = [] + while len(stack) < 32: + stack.append(n % 2) + n //=2 + + result, scale = 0, 1 + while stack: + result += stack.pop() * scale + scale *= 2 + + return result diff --git a/two-sum/jinah92.py b/two-sum/jinah92.py new file mode 100644 index 000000000..e8b204155 --- /dev/null +++ b/two-sum/jinah92.py @@ -0,0 +1,12 @@ +# O(n) time, O(n) space + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_set = {} + + for idx, num in enumerate(nums): + other_num = target - num + if other_num in num_set: + return [idx, num_set[other_num]] + else: + num_set[num] = idx