leetcode 16. 3Sum Closest JAVA
题目:
给定一个包括n个整数的数组nums和一个目标值target。找到nums中的三个整数,使得他们之和与target最为接近。返回三个整数之和,假定每组输入只存在唯一答案

解题思路:
将nums数组重新排列,然后从i = 0开始遍历,分别取left = i + 1和right = nums.length - 1,将nums[i]、nums[left]和nums[right]三数之和与target差值的绝对值与diff比较,如果小于diff,则使sum赋值为此三数之和,并且将diff的值修改为三数之和与target差值的绝对值
class Solution {
public int threeSumClosest(int[] nums, int target) {
int diff = Integer.MAX_VALUE, result = 0;
Arrays.sort(nums);
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 (sum == target) {
return sum;
}
if (diff > Math.abs(target - sum)) {
diff = Math.abs(target - sum);
result = sum;
}
if (sum > target) { //因为sum > target,因此只需将right向左调整,此时sum的值则会减少,这样才能找到更接近的target的三数之和
right--;
} else {
left++; //因为sum <= target,因此只需将left向右调整,此时sum的值则会增加,这样才能找到更接近target的三数之和
}
}
}
return result;
}
}
leetcode 16. 3Sum Closest JAVA的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- 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 giv ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the 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 num ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 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 num ...
- [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 num ...
随机推荐
- requesth获取参数
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) t ...
- script放置最佳位置(转载)
html文件是自上而下的执行方式,但引入的css和javascript的顺序有所不同,css引入执行加载时, 程序仍然往下执行,而执行到<script>脚本是则中断线程,待该script脚 ...
- 【307】◀▶ Python 相关功能实现
目录: 1. Python 实现下载文件 2. 删除文件名中的点 “.” 3. 让 Python 脚本暂停执行的方法 4. 添 1. Python 实现下载文件 使用 urllib 模块提供的 url ...
- inline-block元素出现位置错位的解决方法
如下代码所示: <div class="container"> <div style="display: inline-block; height: 1 ...
- curl 访问 k8s
curl https://mhc:6443/api --cacert ssl/ca.crt --key client_ssl/cs_client.key --cert client_ssl/cs_cl ...
- ShadowVolume
[ShadowVolume] 1.z-pass 算法. z-pass 是 shadow volume 一开始的标准算法,用来确定某一个象素是否处于阴影当中.其原理是: Pass1:enable z-b ...
- cdoj31-饭卡(card) (01背包)
http://acm.uestc.edu.cn/#/problem/show/31 饭卡(card) Time Limit: 3000/1000MS (Java/Others) Memory ...
- UGUI防止穿透和判断点击的是否是UI
用UGUI做的UI,比如按钮,点击一下,后面的3D物体也会接收到点击事件! 1.UGUI自带的防穿透代码: if (EventSystem.current.IsPointerOverGameObjec ...
- 快速上手Runtime(二)之给分类添加属性
我们都知道,分类是不能直接添加属性的,那么我们有时候又需要实现这个功能,那么我们应该怎么办才能为分类添加上属性呢. Runtime给分类添加属性原理 给一个类声明属性,其实本质就是给这个类添加关联,并 ...
- Python open()文件处理使用介绍
1. open()语法open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])open函数有很多 ...