Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- # 1. two pointer 구현. hash table 사용하면 공간 복잡도 O(n)이라서 이렇게 함.
- # Time complexity : O(n)
- # Space complexity : O(1)
- def twoSum(self, numbers, target):
- """
- :type numbers: List[int]
- :type target: int
- :rtype: List[int]
- """
- idx1, idx2 = 0, len(numbers)-1
- while idx1 < idx2:
- cur_sum = numbers[idx1] + numbers[idx2]
- # print(idx1, idx2, cur_sum)
- if cur_sum == target:
- return [idx1+1, idx2+1]
- elif cur_sum > target:
- idx2 -= 1
- else:
- idx1 += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement