요즘 리트코드의 원샷원킬에 재미가 들렸다. 물론 난이도가 Easy라서 가능한거지만^^
오늘 문제도 무난했다. 두개의 포인터를 써서 Binary search와 같은방법으로 접근했더니,,,
Test case 돌리기도 전에 잘못 눌러서 Submit했는데 Accept되었다. 기분이 좋다 힛
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
# Two pointers
n = len(numbers)
low = 0
high = n - 1
while low <= high:
sums = numbers[low] + numbers[high]
if sums < target:
low += 1
elif sums > target:
high -= 1
else: # sums == target
return [low + 1, high + 1]
return [low + 1, high + 1]
문제에서는 정확히 하나의 답이 존재한다는 것을 가정한다고 하였다. 그렇지만 while이나 for문 마지막에 return 쓰는 것이 습관이 되어있는걸요...? 그래서 마지막에도 return을 했답니다.ㅎㅎ
오늘도 끝! 이제 즐거운 연구실 작업으로 고고씽
+) 근데 태그 다는데 투 썸 하니까 투썸플레이스가 생각난다. 히히
'Leetcode' 카테고리의 다른 글
Leetcode 876. Middle of the Linked List (0) | 2021.10.12 |
---|---|
Leetcode 557. Reverse Words in a String III (0) | 2021.10.08 |
Leetcode 283. Move Zeroes (0) | 2021.10.06 |
Leetcode 189. Rotate Array (0) | 2021.10.05 |
Leetcode 977. Squares of a Sorted Array - Two Pointers (0) | 2021.10.04 |