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 ...
随机推荐
- Python super使用
一 基础使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: #!/usr ...
- 【LSGDOJ1834 Tree】树链剖分
题目描述 给定一个N个结点的无向树,树中的结点按照1...N编号,树中的边按照1...N − 1编号,每条边都赋予一个权值.你需要编写程序支持以下三种操作: 1. CHANGE i v:将i号边 ...
- 17.10.31&11.01
10.31模拟考试 Prob.1(AC)裸的矩阵幂 Prob.2(WA)(类似括号匹配求合法方案数) 卡特兰数的一个模型运用.可以推出一个式子(推导方法一个erge讲的,一个骚猪讲的) Prob.3( ...
- hdu 5119(2014北京)
题意: 随机选择一个数,如果后面有比他小的就进行交换,直到没有为止(算一轮).求多少轮后为递增序列 思路: 倒着找,如果有比经过的最小数大的,ans+1 #include <iostream&g ...
- [BZOJ]1089 严格n元树(SCOI2003)
十几年前的题啊……果然还处于高精度遍地走的年代.不过通过这道题,小C想mark一下n叉树计数的做法. Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该 ...
- 51Nod 1753 相似子串
题目大意: 两个字符串相似定义为: 1.两个字符串长度相等 2.两个字符串对应位置上有且仅有至多一个位置所对应的字符不相同 给定一个字符串,每次询问两个子串在给定的规则下是否相似.给定的规则指每次给出 ...
- ssh远程登陆
ssh远程登录命令简单实例 ssh命令用于远程登录上Linux主机. 常用格式:ssh [-l login_name] [-p port] [user@]hostname 更详细的可以用ssh ...
- gray-code (格雷码)
题目描述 The gray code(格雷码) is a binary numeral system where two successive values differ in only one bi ...
- 数据挖掘_requests模块的get方法
关于requests模块 之前在跟大家讲通过字典列表批量获取数据的时候用过这个模块 安装过程就不再讲解了 requests模块是python的http库,可以完成绝大部分与http应用相关的工作,所以 ...
- 如何用cmd通过sublime打开文件?
sublime 提供了专门的命令工具subl.exe,就在它的安装目录之下,讲安装目录配置到系统环境变量中就OK了.具体如下: 1.找到sublime安装路径 如我的默认路径:C:\Program F ...