LeetCode-3Sum Closest-三数和最近-有序数组逼近

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

和3sum类似。不同的是这次需要逼近一个值,实际上跟相等类似,用l和r指针不断移动,然后反复取最小即可。

class Solution {
public:
    int n,m;
    int threeSumClosest(vector<int> &num, int target) {
        vector<int> &a=num;
        n=a.size();
        sort(a.begin(),a.end());
        int ms=numeric_limits<int>::max();
        int res=-1;
        for(int i=0;i<n;i++){
            int x=a[i];
            int l=i+1,r=n-1;
            while(l<r) {
                int cs=x+a[l]+a[r];
                if (abs(cs-target)<ms) {
                    res=cs;
                    ms=abs(cs-target);
                }
                if(cs <target) l++;
                else r--;
            }
        }
        return res;
    }
};
时间: 2024-08-25 18:15:41

LeetCode-3Sum Closest-三数和最近-有序数组逼近的相关文章

259 [LeetCode] 3Sum Smaller 三数之和较小值

题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. For example, given nums = [-2, 0, 1, 3], and target = 2.

[leetcode]3Sum Closest @ Python

原题地址:http://oj.leetcode.com/problems/3sum-closest/ 题意:数组中每三个元素进行求和,找出所有和中大小最接近target的和,并返回这个和与target之间的差值. 解题思路:使用一个变量mindiff来监测和与target之间的差值,如果差值为0,直接返回sum值. 代码: class Solution: # @return an integer def threeSumClosest(self, num, target): num.sort()

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

Leetcode(4)寻找两个有序数组的中位数

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1* 和 nums2 不会同时为空. 第一种方法:list拼接排列取中位数 执行用时:116 ms : 内存消耗:11.8MB 效果:还行 class Solution(object): def findMedianSortedArrays(self,

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

23.3Sum(三数和为零)

Level: ??Medium 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Exampl

LeetCode 88. Merge Sorted Array(合并有序数组)

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi

[LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 这道题是之前那道Remove Duplicates from Sorted Array 有序数组中