LeetCode 16 3Sum Closest (最接近target的3个数之和)
package leetcode_50; import java.util.Arrays; /***
*
* @author pengfei_zheng
* 最接近target的三个数组合
*/
public class Solution16 {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);//排序操作
int result = nums[0]+nums[1]+nums[nums.length-1];
for(int i=0;i<nums.length-2;i++){//遍历
int start = i+1,end = nums.length-1;
while(start<end){//移动两端指针
int sum = nums[i]+nums[start]+nums[end];
if(sum>target)//移动end指针
end--;
else start++;//移动start指针
if(Math.abs(sum-target)<Math.abs(result-target))
result=sum;//更新result值
}
}
return result;
}
}
LeetCode 16 3Sum Closest (最接近target的3个数之和)的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [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(指针搜索)
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 S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- [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 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- php写文件操作
function writeLog($file, $msg, $mode='a+') { $fp = fopen($file, $mode); if(flock($fp, LOCK_EX)) { fw ...
- matlabr2015b安装教程
R2015b MATLAB破解版安装教程 MATLAB和Mathematica.Maple并称为三大数学软件.它在数学类科技应用软件中在数值计算方面首屈一指.MATLAB可以进行矩阵运算.绘制函数和数 ...
- Linux远程管理之SVN,VNC
一.远程管理的基本概念 首先我们来初略的讲讲远程管理的一些基本概念.对于我们使用的计算机来说,如果是个人计算机,就没有远程管理这一概念了,想用的时候开机就能使用,而对于我们的服务器来说,就不同了,对于 ...
- 上机题目(0基础)- 用数组实现记事本(Java)
用java实现一个记事本程序,记录记下的按键,代码例如以下: package com.java.test; import java.awt.Graphics; import java.awt.even ...
- Java从控制台接受输入字符
创建一个类,在该类的主方法中创建Scanner扫描起来封装System类的in输入流,然后提示用户输入身份证号码,并输入身份证号码的位数. 代码如下: import java.util.Scanner ...
- Binary Numbers
时空限制 Time Limit:1000ms Resident Memory Limit:1024KB Output Limit:1024B 题目内容 Given a positive integer ...
- SpringMVC -- 梗概--源码--贰--mvc:annotation-driven
1>在springMVC的处理流程中,有两个重要组件:HandlerMapping和HandlerAdapter 分别负责解析Handler和执行Handler 2>如果配置了<mv ...
- Web实时通信之Socket.IO
前面两篇文章使用了Ajax long polling和WebSocket两种常用的Web实时通信方式构建了简单的聊天程序. 但是,由于浏览器的兼容问题,不是所有的环境都可以使用WebSocket这种比 ...
- 关于openssl的编译与使用
关于openssl的编译与使用,可以参考这两往篇文章 http://blog.csdn.net/lazyclough/article/details/7456131 http://www.leaves ...
- python中安装dlib和cv2
这两个模块是很容易出问题的模块,以下的解决办法都是从网上收集而来. 安装dlib: pypi.python.org/pypi/dlib/19.6.0 下载 dlib-19.6.0-cp36-cp36m ...