LeetCode16:3Sum Closest

2024-01-20 08:08
文章标签 3sum closest leetcode16

本文主要是介绍LeetCode16:3Sum Closest,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

思路:先将数组排序,固定第一个数k,然后设定两个指针i和j,i从k+1开始,j从nums.length-1开始,每次求nums[i]+nums[j]+nums[k]到target的距离,即绝对值。用一个变量保存距离,一个变量保存三个数的和,每次与之前的距离比较,如果比之前的距离小,则更新三个数的和,同时更新距离,如果nums[i]+nums[j]+nums[k]大于target,则将j左移,如果nums[i]+nums[j]+nums[k]小于target,则将i右移,以此缩小nums[i]+nums[j]+nums[k]与target的距离。

代码:

public class ThreeSumClosest {public static void main(String[] args) {int[] nums={-1, 2, 1, -4};System.out.println(threeSumClosest(nums,1));}public static int threeSumClosest(int[] nums, int target) {Arrays.sort(nums);int closest = Integer.MAX_VALUE;int abs = Integer.MAX_VALUE;for (int i = 0; i < nums.length-2; i++) {int left = i+1,right=nums.length-1;while(left<right){int sum = nums[i]+nums[left]+nums[right];if(Math.abs(sum-target)<abs){closest = sum;abs = Math.abs(sum-target);}if(sum-target<0)left++;else if(sum-target>0)right--;elsereturn sum;}}return closest;}
}

输出:2

这篇关于LeetCode16:3Sum Closest的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/625322

相关文章

LeetCode - 15. 3Sum

15. 3Sum Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数列,找出这个数列中和为0的三元组. analyse: 时间复杂度:O(n^2) 思路很简单,注意一下去重的方法. Time comple

LeetCode 16 3Sum Closest

题意: 给出数组s和目标target,要求从中选出3个数字,使其相加的和最接近target。 思路: x sum系列的题目,在这里做一个总结。 最经典的情况为2 sum问题,即给出s和target找出s[i] + s[j] = target。 可以使用枚举s[i]判断target - s[i]是否在s中出现且与s[i]不同的O(nlogn)方法,用map或排序后二分查找的方式均可。

Closest Leaf in a Binary Tree

Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree:1/ \2 3/4/5/6Output: 3Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the no

3Sum java leetcode

Given an array S of n integers, are there elementsa,b, c in S 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 dupl

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

Find K Closest Elements问题及解法

问题描述: Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are al

【LeetCode最详尽解答】15-三数之和 3sum

欢迎收藏Star我的Machine Learning Blog:https://github.com/purepisces/Wenqing-Machine_Learning_Blog。如果收藏star, 有问题可以随时与我交流, 谢谢大家! 链接: 15-三数之和 直觉 示例: 输入: nums = [-1, 0, 1, 2, -1, -4] 输出: [[-1, -1, 2], [-1

Leet Code Medium 15 3Sum

【题目】 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet

[LeetCode] 015--3Sum --Medium--

3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c)

Leetcode 3171. Find Subarray With Bitwise AND Closest to K

Leetcode 3171. Find Subarray With Bitwise AND Closest to K 1. 解题思路2. 代码实现 题目链接:3171. Find Subarray With Bitwise AND Closest to K 1. 解题思路 这道题坦率地说让我感觉很挫败,又一次没有自力搞定,是看了大佬们的答案才搞定的…… 知道比没有搞定更难受的是什么吗?是连