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 ...
随机推荐
- IO流大总结
- - - - - - - - - - - - - - - 写在前面 - - - - - - - - - - - - - - - 1.概念 IO流用来处理设备之间的数据传输 Java对数据的操作是通过 ...
- [Vijos 1143]三取方格数
Description 设有N*N的方格图,我们将其中的某些方格填入正整数, 而其他的方格中放入0. 某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角. 在走过的路上,他取走了方格中的数 ...
- 洛谷3794 签到题IV
题目描述 给定一个长度为n的序列$a_1,a_2...a_n$,其中每个数都是正整数. 你需要找出有多少对(i,j),$1 \leq i \leq j \leq n$且$gcd(a_i,a_{i+1} ...
- ●BZOJ 4318 OSU!
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4318题解: 期望dp 如果我们能够得到以每个位置结尾形成的连续1的长度的相关期望,那么问题就 ...
- ●BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题解: LCT 如果把弹跳的起点和终点连一条边,弹出去的与n+1号点连边, 则不难发现 ...
- UVA1658:Admiral
题意:给定一个有向带权图,求两条不相交(无公共点)的路径且路径权值之和最小,路径由1到v 题解:这题的关键就在于每个点只能走一遍,于是我们想到以边换点的思想,用边来代替点,怎么代替呢? 把i拆成i和i ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- ●BZOJ 3126 [Usaco2013 Open]Photo
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...
- ●BZOJ 4826 [Hnoi2017]影魔
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4826 题解: 主席树,单调栈 以前还没做过这种维护信息的题,感觉好奇妙. 每对相邻的两个数所 ...
- Nginx+uWSGI+Django环境配置
通常项目会部署在虚拟环境,虚拟环境的使用可以参考这里,点击前往 当然你也可以直接部署,这里不多说. 一.安装uWSGI 1.通过pip安装 pip install uwsgi 这里只说明了一种安装方式 ...