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 ...
随机推荐
- 【mvrp多协议vlan注册协议给予三种注册方式的验证】
MVRP 多vlan注册协议给予三种注册模式的配置 一:根据项目需求搭建好拓扑图如下 二:配置: 首先对项目做理论分析,sw1,sw2,sw3所组成的直连网络中,为使不同的PC之间进行通信,按vlan ...
- JetBrains PyCharm 2017.3注册码
JetBrains PyCharm 2017.3注册码 (1)在激活界面的License server输入:http://idea.liyang.io:或者:点击help→Register→Licen ...
- 处理laravel表单提交默认将空值转为null的问题
比如表单提交,如果我们提交了这个字段,但是这个字段为空字符串.在Laravel中会自动转义成Null. 处理这个问题,直到找到中间件\vendor\laravel\framework\src\Illu ...
- php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:
php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种: 方法一:使用copy函数 格式:copy(source,destination) 将文件从 source ...
- ctf题目writeup(2)
2019.1.29 题目地址: https://www.ichunqiu.com/battalion 1. 点开链接: include "flag.php";$a = @$_REQ ...
- go web处理上传
要使表单能够上传文件,第一步就是添加form的enctype属性,enctype属性有如下三种情况: application/x-www-form-urlencoded 表示在发送前编码所有字符(默认 ...
- Hbase数据IO
场景及方案分析 场景1:logs --> HBase logs -> flume -> hfile -> import -> HBase (实时) csv导入HBase ...
- WCF入门三[WCF宿主]
一.概述 WCF程序必须在宿主上运行,也就是WCF服务必须寄宿在某一个windows的进程中,可以是IIS.控制台程序.窗体程序.WAS以及所有.net程序等程序进程中.在我用VS2013创建WCF服 ...
- gp更新来的太快
意外总是会发生 添加一个判断function的分支,过滤掉function,其实也考虑再进一步去分析它的作用,稍后再议. 更新一下 var gnp = { get: function(url) { r ...
- 深入理解计算机系统(1)--hello world程序的生命周期
第一篇笔记的主题是讨论Hello World程序的生命周期,程序是最简单的hello world程序,使用高级C语言编写. 先介绍整个生命周期中涉及到的几个部分以及相应的概念,然后总结整个生命周期,最 ...