4-sum问题
给定一个整数数组,判断能否从中找出4个数a、b、c、d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对。
- #define SIZE 10
- void judgeAndPut(int* arr, int fix1, int fix2, int begin, int end) {
- while (begin < end) {
- int sum = arr[begin] + arr[end] + arr[fix1] + arr[fix2];
- if (sum > 0) {
- end--;
- } else if (sum < 0) {
- begin++;
- } else {
- cout << " " << arr[fix1] << " " << arr[fix2] << " " << 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 findFourSumEq0(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 - 3; ++i) {
- if (i != 0 && arr[i] == arr[i - 1]) {
- continue;
- }
- for (int j = i + 1; j < SIZE - 2; ++j) {
- if (j != 1 && arr[j] == arr[j - 1]) {
- continue;
- }
- judgeAndPut(arr, i, j, j + 1, SIZE - 1);
- }
- }
- }
4-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文件结构与import用法
首先上一张总结图: 在pycharm中,一般不会将当前文件目录自动加入自己的sourse_path.如果遇到无法import同级目录下的其他模块, 右键make_directory as-->S ...
- [BZOJ 4919]大根堆
Description 题库链接 给定一棵 \(n\) 个节点的有根树,每个点有一个权值 \(val_i\) .你需要选择尽可能多的节点,使得:对于任意两个点 \(i,j\) ,如果 \(i\) 在树 ...
- [HAOI2008]下落的圆盘
Description 有n个圆盘从天而降,后面落下的可以盖住前面的.求最后形成的封闭区域的周长.看下面这副图, 所有的红 色线条的总长度即为所求. Input 第一行为1个整数n,N<=100 ...
- bzoj 2560: 串珠子
Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不 ...
- 智能指针之 auto_ptr
C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理,该智能指针在C++11中已经被弃用,转而由unique_ptr替代, 那这次使用和实现,就具体讲一下auto_pt ...
- kafka快速入门
一.kafka简介 kafka,ActiveMQ,RabbitMQ是当今最流行的分布式消息中间件,其中kafka在性能及吞吐量方面是三者中的佼佼者,不过最近查阅官网时,官方与它的定义为一个分布式流媒体 ...
- 使用WebStorm进行javascript调试
曾经的选择是使用火狐浏览器的Firebug插件,具体的用法到时候在细说,这篇文章登场的是开发静态网页及javascript的利器--webstorm. 一.相关软件安装和配置 安装WebStorm ...
- const的一些用法和理解
首先先说一下const常量的用处,我们知道宏定义#define是没有数据类型的,编译器在编译的时候,不会对宏常量进行类型检查,只进行简单的字符串替换,字符串替换时极易产生意想不到的错误,所以这个时候, ...
- jenkins更新后出现JNLP-connect,JNLP2-connect警告
在更新jenkins后出现提示 This Jenkins instance uses deprecated protocols: JNLP-connect,JNLP2-connect. It may ...
- Eclipse 一直不停 building workspace完美解决总结
一.产生这个问题的原因多种1.自动升级 2.未正确关闭 3.maven下载lib挂起 等.. 二.解决总结(1).解决方法 方法1.修改eclipse启动文件 eclipse.ini ...