Leetcode 16. 3Sum Closest

Python 3 实现:

源代码已上传 Github,持续更新。

"""
16. 3Sum Closest

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

    For example, given array S = {-1 2 1 -4}, and target = 1.

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

class Solution:

    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """

        nums.sort()
        size = len(nums)
        dic = {}
        arr = []
        for i in range(size - 2):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            left = i + 1
            right = size - 1

            diff = 0
            while left < right:
                if left > i + 1 and nums[left] == nums[left - 1]:
                    left += 1
                    continue

                if right < size - 1 and nums[right] == nums[right + 1]:
                    right -= 1
                    continue

                s = nums[left] + nums[right] + nums[i]
                diff = s - target
                if diff == 0:
                    return nums[left] + nums[right] + nums[i]
                else:
                    if abs(diff) not in dic:
                        dic[abs(diff)] = [nums[left], nums[right], nums[i]]
                        arr.append(abs(diff))

                    if diff > 0:
                        right -= 1
                    else:
                        left += 1

        arr.sort()
        return (dic[arr[0]][0] + dic[arr[0]][1] + dic[arr[0]][2])


if __name__ == '__main__':
    solution = Solution()
    print(solution.threeSumClosest([0, 1, 2], 3))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容