leetcode-algorithms-16 3Sum Closest
leetcode-algorithms-16 3Sum Closest
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
解法
解法同题15
class Solution
{
public:
int threeSumClosest(vector<int>& nums, int target)
{
// If less then 3 elements then return their sum
while (nums.size() <= 3)
{
return std::accumulate(nums.begin(), nums.end(), 0);
}
std::vector<int> v(nums.begin(), nums.end());
std::sort(v.begin(), v.end());
int sum = 0;
int n = v.size();
int ans = v[0] + v[1] + v[2];
for (int i = 0; i < n-2; i++)
{
int j = i + 1;
int k = n - 1;
while (j < k)
{
sum = v[i] + v[j] + v[k];
if (abs(target - ans) > abs(target - sum))
{
ans = sum;
if (ans == target)
return ans;
}
(sum > target) ? k-- : j++;
}
}
return ans;
}
};
空间复杂度: O(n^2).
时间复杂度: O(1)
leetcode-algorithms-16 3Sum Closest的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- Leetcode Array 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 ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
随机推荐
- 简单数论总结1——gcd与lcm
并不重要的前言 最近学习了一些数论知识,但是自己都不懂自己到底学了些什么qwq,在这里把知识一并总结起来. 也不是很难的gcd和lcm 显而易见的结论: 为什么呢? 根据唯一分解定理: a和b都可被分 ...
- poi导出excel 并处理插入网络图片 范例 处理文件下载中文乱码
package com.inborn.inshop.controller.product; import com.inborn.inshop.common.util.DateUtils;import ...
- Latex: 保持参考文献大小写
参考: BibTeX loses capitals when creating .bbl file Latex: 保持参考文献大小写 在排版时,BibTeX会根据参考文献的格式将除了title中的第一 ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- wow.js
一.首先说明一下怎么使用这个插件: 1.wow.js依赖于animate.css,首先在头部引用animate.css或者animate.min.css. <link rel="sty ...
- 【Ruby】【变量】
知识点[Ruby 中$开头的全局变量.内部变量.隐藏变量介绍] Ruby 中充满了一系列的隐藏变量,我们可以从这些预定义的全局变量中获取一些有意思的信息. 全局进程变量 $$ 表示当前运行的 ruby ...
- command not found shell returned 127
在 vim 修改某个文件后,退出时,报了如此一个错误.日志如下: 并不是什么大问题,只是在刚入坑 ssh 时,真的被人代入坑里了. # 强制退出并保存 :wq! 不是 :!wq,不知道有没有有缘的小伙 ...
- SpringBoot配置Aop demo
1. Demo部分 package com.example.demo.controller; import org.springframework.web.bind.annotation.Reques ...
- JavaEE编程实验 实验1 Java常用工具类编程(未完成)
1.使用String类分割split将字符串“Solutions to selected exercises can be found in the electronic document The T ...
- django核心配置项
Django的默认配置文件中,包含上百条配置项目,其中很多是我们‘一辈子’都不碰到或者不需要单独配置的,这些项目在需要的时候再去查手册. 强调:配置的默认值不是在settings.py文件中!不要以为 ...