Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. class Solution(object):
  2. # 1. two pointer 구현. hash table 사용하면 공간 복잡도 O(n)이라서 이렇게 함.
  3. # Time complexity : O(n)
  4. # Space complexity : O(1)
  5. def twoSum(self, numbers, target):
  6. """
  7. :type numbers: List[int]
  8. :type target: int
  9. :rtype: List[int]
  10. """
  11. idx1, idx2 = 0, len(numbers)-1
  12. while idx1 < idx2:
  13. cur_sum = numbers[idx1] + numbers[idx2]
  14. # print(idx1, idx2, cur_sum)
  15. if cur_sum == target:
  16. return [idx1+1, idx2+1]
  17. elif cur_sum > target:
  18. idx2 -= 1
  19. else:
  20. idx1 += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement