[双指针] leetcode 16 3Sum Closest

problem:https://leetcode.com/problems/3sum-closest/

xxsum是一个系列的题了,一般有二分搜索和哈希两种方法,这道题要求的是接近值,所以哈希就不起作用了,只能使用二分搜索。

对于排好序的数组,首先确定三个数位置在中间的那个数字,然后在它左右搜索另外两个数,如果大于目标,则右指针左移,否则左指针右移。

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {

        int n = nums.size();
        sort(nums.begin(), nums.end());
        int offset = INT_MAX;
        int res = 0;
        for (int k = 1; k < n - 1; k++)
        {
            int i = 0;
            int j = n - 1;
            while (i < k && j > k)
            {
                int num = nums[k] + nums[i] + nums[j];
                if (abs(target - num) < offset)
                {
                    offset = abs(target - num);
                    res = num;
                }
                if (num == target)
                {
                    return target;
                }
                else if (num > target)
                {
                    j--;
                }
                else
                {
                    i++;
                }
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11281457.html

时间: 2024-11-05 18:37:03

[双指针] leetcode 16 3Sum Closest的相关文章

Leetcode 16. 3Sum Closest(指针搜索)

16. 3Sum Closest Medium 131696FavoriteShare 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 e

leetCode 16.3Sum Closest (离给定值最近的三数之和) 解题思路和方法

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 arr

Java [leetcode 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 =

[LeetCode] 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

Array + two points leetcode.16 - 3Sum Closest

题面 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. 给定数组,找出并返回最接近target的

LeetCode #16 3Sum Closest (M)

[Problem] 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

LeetCode 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

[leetcode]16. 3Sum Closest最接近的三数之和

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 nu

LeetCode 16 3Sum Closest(C,C++,Java,Python)

Problem: 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