【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 ...
随机推荐
- luogu P1056 排座椅
题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同学上课时会交头接耳.同学 ...
- bzoj 4774: 修路
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 282 Solved: 132[Submit][Status][Discuss] Descriptio ...
- 【kmp算法】uva11475 Extend to Palindrome
就把每个串倒过来,和原串匹配,看最后能匹配多少就行. #include<cstring> #include<algorithm> #include<cstdio> ...
- 【动态规划】bzoj1270 [BeijingWc2008]雷涛的小猫
暴力dp是n^2*m的……我们计算每棵树在每层的答案的时候,都需要计算出从那棵树转移过来最优. 但是我们发现,对一棵树而言,从上面转移过来都是一样的,所以我们可以在计算每棵树在每层的答案的时候,先预处 ...
- @requestBody注解的使用(上)
1.@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是ap ...
- Scut游戏服务器免费开源框架--快速开发(3)
Scut快速开发(3) 1 开发环境 Scut Lib版本:5.2.3.2 需要安装的软件 a) IIS和消息队列(MSMQ) b) 数据库,Sql2005以上版本 ...
- Pressed状态和clickable,duplicateParentState的关系
做Android开发的人都用过Selector,可以方便的实现View在不同状态下的背景.不过,相信大部分开发者遇到过和我一样的问题,本文会从源码角度,解释这些问题. 首先,这里简单描述一下,我遇到的 ...
- Automatic Diagnostic Repository
转载自 http://www.eygle.com/archives/2007/08/11g_auto_diag_repository.html#comments Oracle Database 11g ...
- Oracle API Gateway SOAP到REST协议转换
1.SOAP到REST协议转换 打开policystudio,加入一个policy Container. 搜索extract rest 设置成为start 搜索set message,将url中的变量 ...
- firefox dispatchevent的问题
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8&qu ...