LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/
二.题目大意:
给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的和最接近target。
三.题解:
这道题实质就是3sum(http://www.cnblogs.com/wangkundentisy/p/9079622.html)问题的变形,而且题目假设的是解是唯一的,所以该题甚至都不用考虑重复情况了。所以只需在3sum问题中,判断下三个元素的和与target的差值,并根据差值进行赋值即可。
代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
int differ = 0x7fffffff;//初始化差值
int sum = 0;
if(len < 3)
return 0;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i -1])
continue;
int j = i + 1, k = len - 1;
while(j < k)
{
if(nums[i]+ nums[j] + nums[k] == target)
{
return target;//由于题目假设的是解释唯一的,所以如果遇到正好和为target的情况,可以立马返回
}
else if(nums[i] + nums[j] + nums[k] < target)
{
int temp = target - (nums[i] + nums[j] + nums[k]);
if(temp < differ)//找到离target差异最小的一组数据
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
j++;
}
else
{
int temp = (nums[i] + nums[j] + nums[k]) - target;
if(temp < differ)
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
k--;
}
}
}
return sum;
}
};
由于本题基本与3sum问题一致,所以注意事项直接看3sum问题就行了。
LeetCode——16. 3Sum Closest的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [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 ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [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 ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [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 ...
随机推荐
- Spring mvc下载文件java代码
/** * 下载模板文件 * @author cq */ @RequestMapping("/downloadExcel.do") public ResponseEntity< ...
- htm基础知识,css的链入以及标签分类。
<!DocTYPE> DOC--Document 文档 TYPE 类型 文档类型 告诉浏览器这是什么文件 单标签: meta 设置 charset 设置编码 双标签: 开始 ...
- Web四则混合运算
一.代码1: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...
- scripy
性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. import requests def fetch_async(url): ...
- 杜教BM【转载】
https://blog.csdn.net/qq_36876305/article/details/80275708 #include <bits/stdc++.h> using name ...
- LeetCode - Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- 黑马-Spring与数据库
Spring与数据库 Spring与jdbc 引入dataSource 在客户端 模板编程 类的结构图, 真正干活的是JdbcTemplate(底层实现,操作 excute方法) JdbcTempla ...
- Android USB Host框架
Android 下的usb框架及功能点:https://blog.csdn.net/tianruxishui/article/details/379029591.Android framework中* ...
- MySQL Binlog--MIXED模式下数据更新
在 Mixed 模式下,MySQL 会根据执行的每一条具体的 SQL 语句来区分对待记录的日志形式,也就是在 statement 和 row 之间选择一种.如果SQL语句为UPDATE/DELETE等 ...
- zsh:no matches found 问题解决
解决方法: ~/.zshrc 文件加入: setopt no_nomatch 之后,更新配置 source ~/.zshrc