Skip to content

Commit a32772e

Browse files
committed
Two sum Solution
1 parent df8b9f5 commit a32772e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

reverse-bits/jungsiroo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def reverseBits(self, n: int) -> int:
3+
# TC : O(32)
4+
# SC : O(32)
5+
6+
bit_str = ''.join(reversed(format(n, 'b').zfill(32)))
7+
return int(bit_str, 2)
8+
9+

two-sum/jungsiroo.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
"""
4+
O(n^2) 보다 빠르게 풀기 위한 방법 : 정렬 후 투포인터 이용
5+
Tc : O(nlogn)
6+
Sc : O(n) // queue 생성
7+
"""
8+
n = len(nums)
9+
queue = []
10+
11+
for i in range(n):
12+
queue.append([nums[i], i])
13+
14+
queue.sort()
15+
16+
start, end = 0, n-1
17+
sum_ = queue[start][0] + queue[end][0]
18+
19+
while sum_ != target:
20+
if sum_ > target:
21+
end -= 1
22+
else:
23+
start += 1
24+
sum_ = queue[start][0] + queue[end][0]
25+
26+
return [queue[start][1], queue[end][1]]
27+

0 commit comments

Comments
 (0)