【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 ...
随机推荐
- [CF627D]Preorder Test
题目大意: 一个$n(n\le2\times10^5)$个结点的树,每个结点有一个权值$w_i$.可以任选一点为根,并选择一些结点交换其子结点的顺序,使得该树DFS序上第$m$个结点的权值最大.求最大 ...
- UC算法笔试题
说实话,昨天UC的笔试题基本全是基础,但是太基础,直接导致很多都不能确定了.看来不管找工作还是找实习,一定要复习到位.好在我也一直是抱着打酱油的味道,实习与否不是特别在意,否则真心要鄙视死自己啦. 好 ...
- 使用virtualenv为应用提供了隔离的Python运行环境
在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...
- 如何让vs2017 EF实体生成支持Mysql 和 Oracle?
1.Mysql 安装 MySql Connector/Net https://dev.mysql.com/downloads/connector/net/ 安装 mysql f ...
- 重设Windows 7密码 z
Restart the computer to boot using the CD. Once the GUI loads, press SHIFT+F10 to bring up the comma ...
- Swift,字典
1.创建(Dictionary)字典(无序的可重复) (1)指定类型 var a:Dictionary<String,String>=["a":"b" ...
- Fillrate
http://xionggf.com/articles/graphic/misc/mobile_gpu_term.html IMR Immediate Mode Rendering 立即渲染模式 TB ...
- 关于JS里的函数作用域链的总结
在JavaScript中,函数的作用域链是一个很难理解的东西.这是因为JavaScript中函数的作用域链和其他语言比如C.C++中函数的作用域链相差甚远.本文详细解释了JavaScript中与函数的 ...
- spring注入之使用标签 @Autowired @Qualifier
使用标签的缺点在于必需要有源代码(由于标签必须放在源代码上),当我们并没有程序源代码的时候.我们仅仅有使用xml进行配置. 比如我们在xml中配置某个类的属性 <bea ...
- 基于Spark Mllib,SparkSQL的电影推荐系统
本文测试的Spark版本是1.3.1 本文将在Spark集群上搭建一个简单的小型的电影推荐系统,以为之后的完整项目做铺垫和知识积累 整个系统的工作流程描述如下: 1.某电影网站拥有可观的电影资源和用户 ...