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 ...
随机推荐
- Spring官方文档翻译(转)
http://blog.csdn.net/tangtong1/article/details/51326887 http://blog.csdn.net/tangtong1/article/detai ...
- [FJOI 2014]最短路径树问题
Description 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最 ...
- [HNOI2012]集合选数
题目描述 <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中. 同学们不喜 ...
- UpdateAfterEvent
10月3日,在杭州市西湖景区,一只小松鼠不停地接受一道道食物,花生.玉米.饼干,可谓来者不拒,憨态可掬的模样吸引了众多围观者...Description 小松鼠打了10个小时的游戏,一脸满足.却发 ...
- mysql中binlog与存储引擎的2PC
mysql内部的2PC mysql开启binlog后实际上可以认为其数据有两份,binlog中一份,引擎中一份(这里先把存储引擎中数据看成整体的单独一份,另外也可以把binlog看成是一个引擎).既然 ...
- Angular中 build的时候遇到的错误--There are multiple modules with names that only differ in casing
今天早上遇到一个Angular的编译的时候的错误 具体信息: There are multiple modules with names that only differ in casing.This ...
- 第四节基础篇 - SELECT 语句详解
4.1 基本的SELECT语句 select * from T_WEATHER select cityname from t_weather 4.2 数学符号条件(>.<.>=.&l ...
- js打印小结
<script type="text/javascript"> //打印必备参数 var hkey_root,hkey_path,hkey_key; hkey_root ...
- div,margin,padding
<!-- 类比礼品盒里装方块月饼.月饼的食用部分(我们把它称之为月饼肉身)要装在小包装盒里,月饼肉身即为content.月饼肉身与直接包裹它的小包装盒(我们把它叫做月饼的衣服)之间的距离叫pad ...
- JavaScript判断不同平台
function getPlatformType() { let UA = navigator.userAgent; if(/MicroMessenger/i.test(UA)){ return 'w ...