【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 ...
随机推荐
- SQL Server的三种物理连接之Merge join(二)
简介 merge join 对两个表在连接列上按照相同的规则排序,然后再做merge,匹配的输出. 下面这个动态图展示了merge join的详细过程. merge join示例 创建两个表 IF O ...
- Xcode7网络问题
更新Xcode7以后运行模拟器,控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource ...
- HTML邮件制作规范
以下内容有些是别人总结的,有些是自己在工作中总结的. 模板最佳尺寸:显示宽度550px-750px,模板高度控制在一屏以内. 1. 用table+css方式构建模板 Div+css布局不完全被邮件客户 ...
- Shell根据年月日创建文件夹
#!/bin/sh dir_path="/vol/project/log/test/" ..} do #echo "$year" cd $dir_path mk ...
- 解决ListView滑动时卡的问题,实现异步加载图片解决
ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...
- dbt
Procedure Relocate(s : state; b : base_index) { Move base for state s to a new place beginning at b ...
- Java学习笔记:语言基础
Java学习笔记:语言基础 2014-1-31 最近开始学习Java,目的倒不在于想深入的掌握Java开发,而是想了解Java的基本语法,可以阅读Java源代码,从而拓展一些知识面.同时为学习An ...
- 求小于等于n的所有素数
题目:要求输出所有小于等于n的素数(n>=2,且为正整数) 要求:1.每行输出10个素数 2.尽可能采用较优算法 #include<iostream> #include<cma ...
- ofbiz进阶之框架配置文件指导
The Open For Business Project: Framework Configuration Guide 原文链接:http://ofbiz.apache.org/docs/corec ...
- strcmp的源码实现
微软方法: int __cdecl strcmp (const char *src, const char *dst) { ; while(!(ret = *(unsigned char *)src ...