给定一个整数数组,判断能否从中找出3个数a、b、c,使得他们的和为0,如果能,请找出所有满足和为0个3个数对。

#define SIZE 10
void judgeAndPut(int* arr, int target, int begin, int end) {
	while (begin < end) {
		if (arr[begin] + arr[end] + arr[target] > 0) {
			end--;
		} else if (arr[begin] + arr[end] + arr[target] < 0) {
			begin++;
		} else {
			cout << " " << arr[target] << " " << arr[begin] << " " << arr[end]
					<< endl;
			begin++;
			end--;
			while (begin + 1 < end && arr[begin - 1] == arr[begin]) {
				begin++;
			}
			while (end - 1 > begin && arr[end + 1] == arr[end]) {
				end--;
			}
		}
	}
}

void findThreeSumEq0(int* arr) {
	qsort(arr, SIZE, sizeof(int), myCmp);

	for (int i = 0; i < SIZE; i++) {
		cout << " " << arr[i];
	}
	cout << endl;
	for (int i = 0; i < SIZE - 2; ++i) {
		if (i != 0 && arr[i] == arr[i - 1]) {
			continue;
		}
		judgeAndPut(arr, i, i + 1, SIZE - 1);
	}
}

3-sum问题的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. PHPCMS v9.5.6 通杀getshell(前台)

    漏洞url:http://wooyun.jozxing.cc/static/bugs/wooyun-2014-062881.html 很好的fuzz思路. 文章提到:文件名前面的数字是被"干 ...

  2. ●POJ 1113 Wall

    题链: http://poj.org/problem?id=1113 题解: 计算几何,凸包 题意:修一圈围墙把给出的点包围起来,且被包围的点距离围墙的距离不能小于L,求围墙最短为多少. 答案其实就是 ...

  3. hdu 5600 BestCoder Round #67 (div.2)

    N bulbs  Accepts: 275  Submissions: 1237  Time Limit: 10000/5000 MS (Java/Others)  Memory Limit: 655 ...

  4. 移动端手势双击(MouseDown也可以在移动端响应,但是帧率太低)

    void Update() { if (Input.touchCount > 0)//手指数量 { if(Input.GetTouch(0).phase == TouchPhase.Began ...

  5. Linux基本知识总结

    一.Linux的基本介绍 起源:大家知道先有Unix,后有的linux就行了,其他的细节可以自己查阅资料. 特点:开源!!! 安全(Linux的病毒远少于window). 免费(商业公司最喜欢这一点) ...

  6. C语言程序设计第二次作业——

    1,编译过程过程中的错误缺引号和分号并且拼写错误. 正确结果: 2,编译过程 改正错误: 正确结果: 3,利用SIZEOF运算符求出的数据类型所占字节大小: 4,在头文件LIMITS.H中相关的编译 ...

  7. $.messager.confirm 用法

    <script type="text/javascript">     $(function () {         $.messager.defaults = { ...

  8. 深入理解final关键字

    在了解了final关键字的基本用法之后,这一节我们来看一下final关键字容易混淆的地方. 1.类的final变量和普通变量有什么区别? 当用final作用于类的成员变量时,成员变量(注意是类的成员变 ...

  9. Java HashMap的扩容

    最近博主参加面试,发现自己对于Java的HashMap的扩容过程理解不足,故最近在此进行总结. 首先说明博主德Java为1.8版本 HashMap中的变量 首先要了解HashMap的扩容过程,我们就得 ...

  10. CSS(一)解析浮动塌陷与清除浮动

    清除浮动方法 1.对父级设置适合CSS高度,父级元素撑开并且包含子元素. <p>固定高度</p> <div style="height: 50px;" ...