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 ...
随机推荐
- 【ospf-链路验证】
根据项目需求搭建好拓扑图 配置RT1的环回口IP和G0/0/0IP地址 开启RT1接口ospf认证,配置接口密码为H3C 配置RT1的ospf区域 同理 开启RT2接口ospf认证,配置接口密码为g0 ...
- p标签内不能含有块元素。
原来一直听网上这样说.自己并没有实际遇到过.上例子. <!DOCTYPE html> <html> <head> <meta charset="ut ...
- 关于api接口
前阵子一直疯狂的找关于php的api接口方面的资料来学习,总结了一下,无非就是请求数据,然后返回数据,当然也要设置相关安全措施,比如认证口令 等.返回数据格式是json 还是xml 看自己需求咯
- [原]解决phpstudy下的nginx无法运行的问题
一直在用phpstudy下的apache,今天忽然想切换到nginx,出现了一些错误,最终还是解决了. 之前是php 5.3 + apache 现在是php 5.3n + nginx 问题就出在这n上 ...
- PLC编码规范
PC在编码规范方面比PLC要好很多.既然它们都是编程语言,那么PC方面的规范是否可以用与PLC呢?答案是肯定的,但需要作取舍.下面规范中的大部分可以用于一般PLC,其中有些只是针对西门子博途,使用时需 ...
- MetInfo最新网站漏洞如何修复以及网站安全防护
metinfo漏洞于2018年10月20号被爆出存在sql注入漏洞,可以直接拿到网站管理员的权限,网站漏洞影响范围较广,包括目前最新的metinfo版本都会受到该漏洞的攻击,该metinfo漏洞产生的 ...
- (长期更新)OI常用模板
代码很简单的模板就不收录了. DFT 离散傅立叶变换 void dft(pdd *a,int l,bool r){ int i,j=l/2,k; for(i=1;i<l;++i){ if(i&l ...
- SEARCH(文字の検索)
文字列に関する無効命令 以下の各命令は無効であり.4.6 および 6.10 までのリリースとの互換性を確保するためにのみ利用可能となっています.これらの命令が古いプログラムの中に出現することはあります ...
- java入门---简介&简单输出小例子&开发前准备
Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出.J ...
- Java-Swing中使用Web富文本编辑器
资料下载 (截取出了邮件发送的功能.) 2018/11/10 因为要 win7 电脑 IE 8 的原因,使用了 jxBrower 拓展,更容易使用,参考链接(推荐) 问题介绍 window客户端软件的 ...