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 app02a;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;impo ...
- [CQOI 2010]扑克牌
Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的 牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1 ...
- [SDOI2008]Sue的小球
题目描述 Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当一个海盗,而是要收集空中漂浮的彩蛋,Sue有一 ...
- 【BZOJ 2395】Time is money
题目大意有n个城市(编号从0..n-1),m条公路(双向的),从中选择n-1条边,使得任意的两个城市能够连通,一条边需要的c的费用和t的时间,定义一个方案的权值v=n-1条边的费用和*n-1条边的时间 ...
- HDU - 3037:Saving Beans
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #in ...
- 【AHOI2005】病毒检测
题目描述 科学家们在Samuel星球上的探险仍在继续.非常幸运的,在Samuel星球的南极附近,探险机器人发现了一个巨大的冰湖!机器人在这个冰湖中搜集到了许多RNA片段运回了实验基地. 科学家们经过几 ...
- django rest-framework 4.REST的认证和权限
目前,我们的API对谁可以编辑或删除代码段没有任何限制.我们想要一些更先进的行为,以确保:(这段话抄自官网) 代码段始终与创建者相关联. 只有身份验证的用户可以创建片段. 只有片段的创建者可以更新或删 ...
- Cisco 的基本配置实例之四----vlan的规划及配置(接入交换机)
4.2 接入交换机的相关配置 ## 在此例中,我们联入的是一台接入交换机,此交换机的gi0/1口上联至核心交换机.也就意味着我们需要配置gi0/1为trunk口.具体的配置如下: D-2960-3(c ...
- java.lang.NumberFormatException: For input string: " "
原因:这个异常是说,在将字符串""转换为number的时候格式化错误.额,很简单的异常,以前我是写个方法,然后遍历对比不正确的数字或者用正则表达式之类的.现在发现一个很漂亮的方法, ...
- promise应用及原生实现promise模型
一.先看一个应用场景 发送一个请求获得用户id, 然后根据所获得的用户id去执行另外处理.当然这里我们完全可以使用回调,即在请求成功之后执行callback; 但是如果又添加需求呢?比如获得用户id之 ...