Friday, 1 February 2019

LeetCode Q167 Two Sum II - Input array is sorted

Q167 Two Sum II - Input array is sorted



 Two-pointer solution
 l = 0, r = nums.count - 1,
 check the sum of left and right
 if sum > target, shift r left to try to find a smaller sum
 if sum < target , shift l right to try to find a greater sum
 Until find a sum == target, or no such sum could be found in nums

https://github.com/Sylvia-YiyinShen/Algorithm/blob/master/TwoPointers/Q167TwoSumII%20.playground/Contents.swift

No comments:

Post a Comment