LeetCode OJ-- 3Sum Closest
https://oj.leetcode.com/problems/3sum-closest/
给一列数和target,在这一列数中找出3个数,使其和最接近target,返回这个target。
一般思路是 n*n*n,优化的话,如下:
先给数排序,然后对第一个数进行遍历 i,第二个数为i+1,第三个数为len-1,看得出的和于target的比较。如果小于target,则第二个数下标++,如果大于target,则第三个数下标--。
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int len = num.size();
if(len<)
return ;
if(len == )
return num[]+num[]+num[]; sort(num.begin(),num.end());
int nearAns = num[]+num[]+num[];
for(int i = ;i<len;i++)
{
int j = i+;
if(j>len-)
break;
int k = len-; while(j<k)
{
int sum = num[i]+num[j]+num[k];
if(sum == target)
return target;
if(sum<target)
{
if(abs(target - sum) < abs(target - nearAns))
nearAns = sum;
j++;
}
if(sum>target)
{
if(abs(target - sum) < abs(target - nearAns))
nearAns = sum;
k--;
}
}
}
return nearAns;
}
};
LeetCode OJ-- 3Sum Closest的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 【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][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- [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][Java] 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
随机推荐
- 指向class的指针使用方法实例
// pointer to classes example #include <iostream> using namespace std; class Rectangle { int w ...
- A1042 Shuffling Machine (20)
1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...
- TCP/IP网络编程之地址族与数据序列
分配IP地址和端口号 IP是Internet Protocol(网络协议)的简写,是为收发网络数据而分配给计算机的值.端口号并非赋予计算机的值,而是为区分程序中创建的套接字而分配给套接字的序号 网络地 ...
- 驱动模块 .ko
模块: 模块机制,作用搞高LINUX操作系统的扩充性. 1. 模块概念: 1.动态可加载内核模块LKM 2.内核空间运行 3.是不是一执行文件,是一个没有经过链接,不能独立运行的一个目标文件(.c-& ...
- DEDE调用指定文章ID来调用特定文档
http://www.jb51.net/cms/137423.html 代码如下: {dede:arclist row=1 idlist='6'} <li><a href=" ...
- python - web自动化测试 - 元素操作 - 等待
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: wait.py @ide: PyCharm Community Edi ...
- CentOS7 'Username' is not in the sudoers file. This incident will be reported
新装的 CentOS 需要安装许多软件,但是如果一开始你不是以 root 登入的话,就需要使用 sudo 进行切换,但是通常会报错如下图: 解决方法: 先用 root 用户登入系统, 打开文件 vi ...
- Socket 编程中,TCP 流的结束标志与粘包问题
因为 TCP 本身是无边界的协议,因此它并没有结束标志,也无法分包. socket和文件不一样,从文件中读,读到末尾就到达流的结尾了,所以会返回-1或null,循环结束,但是socket是连接两个主机 ...
- thinkphp框架中使用递归实现无限级分类
无限级分类在我们开发中显得举足轻重,会经常被人问到,而一般会用递归的方法来实现,但是递归又会难倒一批人.今天博主分享的这个稍微有点基础的phper都能学会,希望大家能喜欢. 一.先建立对应的数据库和表 ...
- idea下载多个插件项目启动不了解决方案
今天下载mybatis plugin插件的时候 有好多个版本的plugin,然后呢,看第二个比较热门,就下载了第二个,然后重启idea发现这个插件貌似得花钱,那算了吧,咱用第一个免费的吧,就又下载了第 ...