LeetCode -- 3SumCloset
Question:
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).
Analysis:
与3Sum题目比较起来,这里不需要去重检测,只需要有两个变量控制sum和与target间的差距即可。总体的策略同3Sum。
Answer:
public class Solution {
public int threeSumClosest(int[] nums, int target) {
int tar = Integer.MAX_VALUE;
int res = 0;
Arrays.sort(nums); for(int i=0; i<nums.length-2; i++) {
int j = i + 1;
int k = nums.length - 1; while(j < k) {
int sum = nums[i] + nums[j] + nums[k];
if(sum == target)
return sum;
else {
int dif = Math.abs(sum - target);
if(dif < tar) {
tar = dif;
res = sum;
}
if(sum < target)
j++;
else
k--;
}
}
}
return res;
}
}
LeetCode -- 3SumCloset的更多相关文章
- 2016/10/28 很久没更了 leetcode解题 3sumcloset
16.3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closes ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- doc命令操作数据库(下)
1.给数据表添加一组数据: 2.给数据表添加多组数据: 3.对数据进行删除和修改: 4.用select查询单个或多个数据信息: 5.去除重复值: 6.查询的各种用法: between的用法: 查询排序 ...
- UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】
参考:https://blog.csdn.net/lianai911/article/details/41831645 #include <iostream> #include <c ...
- 【SAPUI5】ODataを構成するもの
はじめに SAPUI5でアプリケーションを作るにあたり.ODataは避けては通れないトピックです.結構広いテーマなので.5-7回くらいに分けて書きたいと思います.1回目はODataの概要について説明し ...
- Altera Stratix IV 命名规则
由于要开发基于DE4平台的应用,应该要了解一下该平台的芯片情况Stratix IV 具体型号为:Stratix IV EP4SGX230KF40C2 命名规范如下 官网资料为:https://www. ...
- Uber:中国市场两年内不考虑盈利,每年补贴10亿美金
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 创龙TMS320C6748开发找不到 tl.dsp.evm6748的问题研究
1. 使用中遇到问题,看了一下帖子说是把tl.dsp.evm6748换成ti.platforms.evm6748可以编译过去.这个包是在XDCtools里面的. js: "D:/ti/ccs ...
- Linux下启动Oracle服务和监听程序步骤
Linux下启动Oracle服务和监听程序启动和关闭步骤整理如下: 1.安装oracle: 2.创建oracle系统用户: 3./home/oracle下面的.bash_profile添加几个环境变量 ...
- Close Java Auto Update in Windows 7 and Later
0. Environment Windows 7JDK 1.6.0_45 1. Steps 1) Enter "JRE/bin" 2) Run javacpl.exe as adm ...
- 【IIS】 常见问题
[IIS] 常见问题 1. IIS 安装 .Net FrameWork 4.0 开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windi ...
- 在阿里云上遇见更好的Oracle(二)
从上一篇文章的反馈来看,大家还是喜欢八卦多过技术细节,那这一篇继续一些题外话,说说我对“去IOE”的看法. 对同一件事情,参与的没参与的人,讨论起来,都会有各自的立场.所以这里先申明一下,以下内容只是 ...