threeSum_0
//找出数组中三个数相加为0,返回存在的组数
//输入指正*A,长度为size,返回*B和长度num
int threeSum_0(int *A,int size,int *B,int &num)
{
vector<int> temp(A, A + size); //使用vector操作数据
sort(temp.begin(),temp.end());//sort
//夹逼
auto last = temp.end();
int *p = new int[];//p为临时数组,大小自己设置
for (auto a = temp.begin(); a <prev(last,); a++){
auto b = next(a);
auto c = prev(last);
while (b < c){
if (*a + *b + *c > )
--c;
else if (*a + *b + *c < )
++b;
else{
*(p++) = *a;
*(p++) = *b;
*(p++) = *c;
num = num + ;
++b;
--c;
}
}
}
p = p - num;
for (int i = ; i < num; i++)
B[i] = p[i];
delete[] p;
return num/; //符合条件的组数
}
threeSum_0的更多相关文章
随机推荐
- php 采集类snoopy http://www.jb51.net/article/27568.htm | cURL、file_get_contents、snoopy.class.php 优缺点
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单. Snoopy的特点: 1.抓取网页的内容 fetch 2.抓取网页的文本内容 (去除HTML标签) fetchtext ...
- TCP协议基础
IP协议是Internet上使用的一个关键协议,它的全称是Internet Protocol,即Internet协议,通常简称IP协议.通过使用IP协议,使Internet·成为一个允许连接不同类型 ...
- ionicModal中的监听事件
//添加监听事件angular.module('MyApp').directive('gotTapped', ['$ionicGesture', function($ionicGesture) { r ...
- android webview 漏洞背后的节操
by superhei 2013/09/06 [注:本文提到的都是我个人的观点,该行为也是私人行为,与任何组织.公司无关.另:水军请自重!] 一.前言 这两天,一个2+年前的android web ...
- iOS App创建桌面快捷方式
http://www.cocoachina.com/ios/20150827/13243.html 先mark,暂时用不到
- ARC 类型转换:显式转换 id 和 void *
http://blog.csdn.net/chinahaerbin/article/details/9471419 /* * ARC有效时三种类型转换: */ 1.__bridge ...
- JDBC 元数据 事务处理
使用 JDBC 驱动程序处理元数据 Java 通过JDBC获得连接以后,得到一个Connection 对象,可以从这个对象获得有关数据库管理系统的各种信息,包括数据库中的各个表,表中的各个列,数据类型 ...
- sourcetree使用问题汇总
1.可优先查阅博文<git 用户手册 1.5.3及后续版本使用>: 2.问题1 Cloning into 'folder'... warning: templates not found ...
- iOS对象序列化
系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了. 我在这里主要介绍自定义对象的归档. Sample.h文件 // // Sample.h // Serialization // // ...
- IOS的变量前加extern和static字段
IOS的变量前加extern和static字段 前一阵子,做项目的时候到网上找Demo,打开运行的时候发现其中变量前有关键字extern和static,所以我研究了一下子 对于extern来说可以理解 ...