【Leetcode】3Sum Closest
来源:互联网 发布:michael angelo 知乎 编辑:程序博客网 时间:2024/06/06 20:20
题目链接: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).
思路:
参考http://blog.csdn.net/yeqiuzs/article/details/50272757 解题框架,本题要求跟target距离最近的3元素之和,所以要保存最小距离、最小距离时候的3元素的和。 这里要搞清楚一点,在判断一个已排序数组中是否存在两元素之和 target的时候,要注意可以从两边向中间逼近target,时空复杂度为O(N),而不是两层for循环两个指针指向两个元素判断是否为target,时空复杂度为O(N^2)。如果是无序数组快排后再向中间逼近是O(N logN),也比O(N^2)好。 本题解法的时间复杂度为O(N^2)。
算法:
1 0
- LeetCode: 3 Sum Closest
- leetcode - 3 sum closest
- leetcode 3Sum Closest
- LeetCode: 3Sum Closest
- [Leetcode]3Sum Closest
- Leetcode: 3SUM Closest
- LeetCode 3Sum Closest
- [Leetcode] 3Sum Closest
- LeetCode : 3Sum Closest
- LeetCode 3Sum Closest
- [LeetCode] 3Sum Closest
- 【leetcode】3Sum Closest
- [LeetCode]3Sum Closest
- LeetCode-3Sum Closest
- LeetCode:3Sum Closest
- LeetCode | 3Sum Closest
- 【leetcode】3Sum Closest
- Leetcode: 3Sum Closest
- 动态规划2-UNIMODAL PALINDROMIC DECOMPOSITIONS(算法基础 第5周)
- 【Leetcode】4Sum
- APP开发实战49-MVP架构
- 单表查询指定字段
- APP开发实战50-MVVM架构
- 【Leetcode】3Sum Closest
- java 之BigInteger类实现计算大整数的阶乘
- (java)Base64 图片编码&&解码
- 【Leetcode】Longest Consecutive Sequence
- 如何筛选连续跌几天的股票?
- 【Leetcode】Pascal's Triangle II
- 简易数码时钟
- 难得的小日记
- Ubuntu下Apache配置SSL笔记