silver-hands / sss

0 stars 0 forks source link

【Q066】面试题57. 和为s的两个数字 #66

Open fly0o0 opened 4 years ago

fly0o0 commented 4 years ago

面试题57. 和为s的两个数字

fly0o0 commented 4 years ago
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
  let sum = 0
  let start = 0
  let end = nums.length - 1

  while (start < end) {

    sum = nums[end] + nums[start]
    if (sum === target) {
      return [nums[start], nums[end]]
    }

    if (sum > target) {
      end--
    } else {
      start++
    }
  }
  return []
};