美文网首页
16. 3Sum Closest {Medium}

16. 3Sum Closest {Medium}

作者: RoyTien | 来源:发表于2019-01-28 21:30 被阅读3次

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

rarara's solution:

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        # IMPORTANT! First, sort the elements
        nums.sort()
        count = len(nums)
        closest_sum = 0
        min_diff = 0xFFFFFFFF

        # Iterate through each of the elements
        for i, n in enumerate(nums):
            left = i + 1
            right = count - 1

            # Use 2 pointers
            # Iterate through the elements to the right of the current element
            while left < right:
                sum = n + nums[left] + nums[right]

                # If sum == target, we are done
                if sum == target:
                    return sum

                elif sum > target:

                    # Find the diff
                    # Store the sum if diff is lesser than current minimum diff
                    diff = sum - target
                    if diff < min_diff:
                        closest_sum = sum
                        min_diff = diff

                    # If sum > target, move right pointer to the left
                    right -= 1

                # If sum < target, move left pointer to the right
                else:

                    # Find the diff
                    # Store the sum if diff is lesser than current minimum diff
                    diff = target - sum
                    if diff < min_diff:
                        closest_sum = sum
                        min_diff = diff

                    # If sum > target, move right pointer to the left
                    left += 1

        return closest_sum

相关文章

  • LeetCode #16 2018-07-28

    16. 3Sum Closest Given an array nums of n integers and an...

  • Day3

    16. 3Sum Closest Given an array S of n integers, find thr...

  • LeetCode 16. 3Sum Closest

    16. 3Sum Closest Given an array S of n integers, find thr...

  • 3Sum Closest

    每日算法——leetcode系列 问题 3Sum Closest Difficulty: Medium Given...

  • 16. 3Sum Closest {Medium}

    Given an array nums of n integers and an integer target, ...

  • 16. 3Sum Closest

    题目: Given an array S of n integers, find three integers i...

  • 16. 3Sum Closest

    题目: 给定一个nums由n个整数和一个整数组成的数组target,找出三个整数nums,使总和最接近target...

  • 16. 3Sum Closest

    Given an array nums of n integers and an integer target, ...

  • 16. 3Sum Closest

    Description Given an array S of n integers, find three in...

  • 16. 3Sum Closest

    Given an array nums of n integers and an integer target, ...

网友评论

      本文标题:16. 3Sum Closest {Medium}

      本文链接:https://www.haomeiwen.com/subject/wokzjqtx.html