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 = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
解题思路:
这个跟3Sum的思路差不多,不过实现的过程就简单多了。外面一个大的循环,里面也是从两边开始,如果三个数之和减去目标值的绝对值较小,则更新之并记录此三个数之和。
代码如下:
public class Solution {
public int threeSumClosest(int[] nums, int target) {
int length = nums.length;
int min = Integer.MAX_VALUE;
int left, right, comp, result = 0; if (length < 3)
return 0;
Arrays.sort(nums); for (int i = 0; i < length - 2; i++) {
left = i + 1;
right = length - 1;
while (left < right) {
comp = nums[i] + nums[left] + nums[right];
if (Math.abs(comp - target) < min){
min = Math.abs(comp - target);
result = comp;
}
if (comp > target)
right--;
else if (comp < target)
left++;
else
return target;
}
}
return result;
}
}
Java [leetcode 16] 3Sum Closest的更多相关文章
- 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 16. 3Sum Closest JAVA
题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...
- [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 ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
随机推荐
- javascript insertBefore 和 appendChild
js的appendChild()方法 :在一个元素内部追加一个子节点. js的insertBefore()方法:在一个元素内部指定的子节点之前插入子节点. 很明显,appendChild()方法只需要 ...
- 【MongoDB】 安装为windows services
参考 http://stackoverflow.com/questions/2404742/how-to-install-mongodb-on-windows # mongodb.conf # da ...
- 微软职位内部推荐-Data Scientist
微软近期Open的职位: Job Description:Extracting accurate, insightful and actionable information from data is ...
- 阿里云Ubuntu服务器安装java环境
一.下载jdk wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-secureb ...
- wget 下载百度云jdk
oracle官网下载需要登录下载 所以从百度云下载 wget -c -O "URL"
- js常识
btnDelAll.Attributes.Add("onclick", "<script lunguage='javascript'>return windo ...
- BZOJ 1706: [usaco2007 Nov]relays 奶牛接力跑
Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...
- [转]LINQ操作数据库
查询表达式(LINQ)简介 C#3.0新语特性和改进,这些新特性在我们编写程序时为我们提供了非常大的帮助.从这篇开始,我们开始一起来探讨LINQ. LINQ是Language Integrated Q ...
- DX 的.x 文件
template Header { <3D82AB43-62DA-11cf-AB39-0020AF71E433> WORD major; WORD minor; DWORD flags;} ...
- DeepFace--Facebook的人脸识别(转)
DeepFace基本框架 人脸识别的基本流程是: detect -> aligh -> represent -> classify 人脸对齐流程 分为如下几步: a. 人脸检测,使用 ...