3-sum问题
给定一个整数数组,判断能否从中找出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问题的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- servlet之隐藏域
隐藏域的实现, 商品对象 package app02b;public class Customer { private int id; private String name; ...
- Ubantu16.04系统优化
系统清理篇 系统更新 安装完系统之后,需要更新一些补丁.Ctrl+Alt+T调出终端,执行一下代码: sudo apt-get update sudo apt-get upgrade 卸载libreO ...
- codevs 搜索题汇总(黄金级)
2801 LOL-盖伦的蹲草计划 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖 ...
- LGTB 与大数
LGTB 有一个非常大的数,并且他想对它进行Q 次操作 每次操作是把这个大数中的某种数字全部替换成一个数字串 他想知道Q 次操作之后得到的数对1000000007(109 + 7) 取模的结果,请输出 ...
- [JLOI2015]装备购买
题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 <= j < ...
- [UOJ] #217. 【UNR #1】奇怪的线段树
题解见大佬博客 我的丑陋代码: #include<cstdio> #include<cstring> #include<cstdlib> inline int re ...
- NOIP 2013
Prob.1 转圈游戏 找到循环节,然后快速幂.代码: #include<cstdio> #include<cstring> #include<iostream> ...
- [POJ2406]字符串的幂
题目描述] 对于给定的两个字符串a,b,我们定义a*b是将把它们连接在一起形成的字符串.例如,若a="abc",b="def",则a*b="abcde ...
- hdu 5130(2014广州 圆与多边形相交模板)
题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y), (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...
- RAC基本原理
RAC基本原理 什么是RAC? 多个实例跑在多个服务器上 一个数据库存放在共享的存储上,所有实例都可以访问 实例之间通过内联网络交换数据和信息 共享存储内容:数据文件.REDO.UNDO.控制文件 参 ...