【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/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).
解题思路:先对数组进行排序,然后遍历数组。寻找和最接近target的数。
演示样例代码:
import java.util.Arrays;
public class Solution
{
public int threeSumClosest(int[] nums, int target)
{
int length=nums.length;
int minNum=Integer.MAX_VALUE;
int tempsubNum=Integer.MAX_VALUE;
Arrays.sort(nums);//先对nums进行排序
for(int i=0;i<length-2;i++)
{
//略过同样的数
if(i!=0&&nums[i]==nums[i-1])
{
continue;
}
int left=i+1;
int right=length-1;
while(left<right)
{
int a1=nums[left];
int a2=nums[right];
int sum=a1+a2+nums[i];
int subNum=Math.abs(sum-target);
//当前的和值sum离target更近一点
if(subNum<=tempsubNum)
{
tempsubNum=subNum; //保存这一步中的sum-target的差值
minNum=sum;
}
if(sum-target<0) //说明sum<target,
left++;
else
right--;
}
}
return minNum;
}
}
【LeetCode OJ 016】3Sum Closest的更多相关文章
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【LeetCode OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- 【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
随机推荐
- 记录(Record)
记录有可以被称为行(Row),可以通俗的认为它是数据表中的一行数据.以员工表为例,一个公司的员工表中的数据是这样的: 这里每一行数据就代表一个员工的资料,这样的一行数据就叫做一条记录.表是由行和列组成 ...
- Oracle的锁
Oracle数据库中的锁机制 数据库是一个多用户使用的共享资源.当多个用户并发地存取数据时,在数据库中就会产生多个事务同时存取同一数据的情况.若对并发操作不加控制就可能会读取和存储不正确的数据,破坏数 ...
- 【DFS序】【莫队算法】【权值分块】bzoj2809 [Apio2012]dispatching
题意:在树中找到一个点i,并且找到这个点子树中的一些点组成一个集合,使得集合中的所有点的c之和不超过M,且Li*集合中元素个数和最大 首先,我们将树处理出dfs序,将子树询问转化成区间询问. 然后我们 ...
- python3开发进阶-Django框架的起飞加速一(ORM)
阅读目录 ORM介绍 Django中的ORM ORM中的Model ORM的操作 一.ORM介绍 1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一 ...
- 1.2(Spring MVC学习笔记) Spring MVC核心类及注解
一.DispatcherServlet DispatcherServlet在程序中充当着前端控制器的作用,使用时只需在web.xml下配置即可. 配置格式如下: <?xml version=&q ...
- 实用类String.length应用-用户名密码长度
package demo3; import java.util.Scanner; //会员注册,用户名长度不小于3,密码长度不小于6,两次输入的密码必须相同 public class Register ...
- 上传--下载HDFS文件并指定文件物理块的大小
使用hdfs的api接口分别实现从本地上传文件到集群和从集群下载文件到本地. 1)上传文件主要是使用FileSystem类的copyFromLocalFile()方法来实现,另外我们上传文件时可以指定 ...
- Linux命令之 file命令
该命令用来识别文件类型,也可用来辨别一些文件的编码格式.它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的. 执行权限 :All User 指令所在路径:/us ...
- Delphi 获取sqlite中所有的表名
取得sqlite数据库里所有的表名 查询table,type 段是'table',name段是table的名字, so: select name from sqlite_master where ty ...
- js之对象(经典)
一.对象的定义: 对象是JavaScript的一个基本数据类型,是一种复合值,它将很多值(原始值或者其他对象)聚合在一起,可通过名字访问这些值.即属性的无序集合. 二.对象的创建(多种方法) 1.对象 ...