//找出数组中三个数相加为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的更多相关文章

随机推荐

  1. LZMA demo挑选使用备忘

    一.源码下载:http://sourceforge.net/projects/sevenzip/ 二.各种EXE及其功能 代码里面有很多的dsw工程,功能不一,size不一,需要挑选合适的加入到自己的 ...

  2. java.io.FileOutputStream类的5个构造方法

    java.io.FileOutputStream的构造函数: ①FileOutputStream(File file) ②FileOutputStream(String name) ③FileOutp ...

  3. [Js]焦点图轮播效果

    一.所用到的知识点 1.DOM操作 2.定时器 3.事件运用 4.Js动画 5.函数递归 6.无限滚动大法 二.结构和样式 <div id="banner" class=&q ...

  4. POJ 1860 Currency Exchange 最短路 难度:0

    http://poj.org/problem?id=1860 #include <cstdio> //#include <queue> //#include <deque ...

  5. C#Base64编码

    一. Base64的编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24 位数据,再把这24位数据 ...

  6. https需要的类

    import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import j ...

  7. ios开发逆向传值的几种方法整理

    第一种:代理传值 第二个控制器: @protocol WJSecondViewControllerDelegate <NSObject> - (void)changeText:(NSStr ...

  8. 支持多人协作的在线免费作图工具:ProcessOn

    之前朋友给我推荐一款作图工具ProcessOn,出于好奇我就研究了一下它,今天我就给大家简单介绍一下这款免费的在线作图工具:ProcessOn 首先使用ProcessOn我们需要有一个帐号,这样每次操 ...

  9. 创建和运行shell脚本程序

    转载请标明http://www.cnblogs.com/winifred-tang94/ 要创建一个shell脚本程序,首先新建一个文本文件,然后在这个文本文件中按照shell编程规则输入shell命 ...

  10. history对象

    1.history对象前进 history.forward() 2.history对象后退 history.back() 3.history对象跳入指定页面 history.go(-1):  //当前 ...