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个数来,使得这三个数的 ...
随机推荐
- 1020. Tree Traversals (序列建树)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- Eclipse 中隐藏的 5 个非常有用的功能
Eclipse就是一头野兽.它也是一套设备,神秘但更具威力.有些人称它为一个持续变形机.另一些人则称它是一个变异体.不错,它很庞大,需要花费多年才能掌握.而在你好不容易掌握之后,你的老板出现了然后告诉 ...
- 第2章 Git安装与设置
2.1 安装Git 略 2.2 设置Git 通过命令git config,用户可以把此类信息提供给本地版本库. 全局变量:在这台计算机上使用任何Git版本库时,这些全局变量的值都起作用.在相关命令中加 ...
- CoInitialize()、CoInitializeEx()和AfxOleInit()区别联系
CoInitialize()和AfxOleInit() 都是初始化COM库,不同之处在与: OLE是建立在COM之上的技术,层次比COM要高.AfxOleInit()调用的是OleInitialize ...
- spring的三种注解管理器
1.依赖注入的注解解析器 在配置文件中; * xsd xmlns:context="http://www.springframework.org/schema/context" h ...
- 【规范】javascript 变量命名规则(转)
匈牙利命名法 语法 变量名 = 类型 + 对象描述 类型指变量的类型 对象描述指对象名字全称或名字的一部分,要求有明确含义,命名要容易记忆容易理解. 通过在变量名前面添加相应小写字母的符号标示作为前缀 ...
- Windows下配置使用 MemCached
Windows下配置使用MemCached 工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manage ...
- SAX EntityResolver 的作用
1.1 何为 EntityResolver : 官方解释: 如果SAX应用程序叙事实现自定义处理外部实体,则必须实现此接口, 并使用setEntityResolver方法向SAX 驱动器注册一个实例. ...
- PAT-乙级-1019. 数字黑洞 (20)
1019. 数字黑洞 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定任一个各位数字不完全相同的4位 ...
- PHP5 session 详解
http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越来越复杂的WEB应用,需要保存一些用户状 ...