【leetcode】3Sum Closest(middle)
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).
思路:
先排序,然后固定一个边界,另外两个边界收缩。
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int l, r, m;
int ans = num[] + num[] + num[];
sort(num.begin(), num.end()); //数字从小到大排序
for(l = ; l < num.size() - ; l++) //固定左边界
{
m = l + ;
r = num.size() - ;
while(m < r) //收缩中间和右边界
{
int tmp = num[l] + num[m] + num[r];
ans = (abs(ans - target) > abs(tmp - target)) ? tmp : ans;
if(tmp < target)
{
(num[m] > num[r]) ? r-- : m++; //比目标小 则把小的数字变大
}
else if(tmp > target)
{
(num[m] < num[r]) ? r-- : m++; //比目标大 则把大的数字变小
}
else
{
return tmp; //已经等于target就直接返回
}
}
}
return ans;
}
};
【leetcode】3Sum Closest(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- Could not delete folder on Win7
I had the same problem on Win 7 and this worked for me in command prompt. Solution 1: RD/S pathname ...
- Setup Project 安装项目
从vs2012起,微软已经不支持setup project了.以此纪念一下setup project. 在新建Setup Project 增加安装内容,通常是直接Oupput一个项目,或者直接 ...
- popViewControllerAnimated 后,对页面内UITableView 内数据刷新
popViewControllerAnimated后,这时它不执行viewDidLoad,所以不能及时对viewControler及时刷新,此时对该页面进行操作可以调用viewWillAppear:( ...
- java线程安全和线程同步
第一部分 线程安全(1)——变量安全 http://blog.csdn.net/cuiran/article/details/6150357 第二部分 线程安全(2)——ThreadLocal变量 h ...
- Android学习1
Activity学习(1) 只有一个Activity 进行Toast通知 Toast是一种短小的提醒,显示一段时间就会消失,试验学习,可以通过一个Button来实现. Button reg=(Butt ...
- 在windows服务器中,将MongoDB服务化。
将mongodb在windows中服务化,就是将其注册成一个服务组件,并可以设置成,手动/自动 启动. 一般的我们都会在command窗口运行如下: d:\mongodb\bin>mongod ...
- js获取url参数值的两种方式
js获取url参数值的方法有很多,下面也为大家介绍两种. 方法一:正则分析法 function getQueryString(name) { var reg = new RegExp(" ...
- 浏览器页面区域大小的js获取方法
浏览器页面区域大小的获取: /在IE.FireFox.Opera下都可以使用 document.body.clientWidth document.body.clientHeight //即可 ...
- Visual C++2010开发权威指南 中文高清PDF - VC.NET
第一部分 Visual C++ 2010开发与新特性第1章 Visual C++ 2010开发环境简介 11.1 Visual C++ 2010简介 11.2 Visual C++ 2010下 ...
- Pandas简易入门(三)
本节主要介绍一下Pandas的数据结构,本文引用的网址:https://www.dataquest.io/mission/146/pandas-internals-series 本文所使用的数据来自于 ...