题1、给定一个int数组,一个数sum,求数组中和为sum的任意2个数的组合

 @Test
public void test_find2() {
int[] arr = { -1, 0, 2, 3, 4, 7, 8, 9, 10 };
int sum = 9;
Arrays.sort(arr);
List<TwoTuple<Integer, Integer>> result = new ArrayList<>(); int i = 0;
int j = arr.length - 1;
while (i < j) {
if (arr[i] + arr[j] == sum) {
result.add(new TwoTuple<Integer, Integer>(arr[i], arr[j]));
i++;
j--;
} else if (arr[i] + arr[j] < sum) {
i++;
} else { // > sum
j--;
}
}
System.out.println(result);
} // out: [-1:10, 0:9, 2:7]

题2、给定一个int数组,一个数sum,求数组中和为sum的任意3个数的组合

 @Test
public void test_find3() {
int[] arr = { -1, 0, 2, 3, 4, 7, 8, 9, 10 };
int sum = 9;
Arrays.sort(arr);
Set<ThreeTuple<Integer, Integer, Integer>> result = new LinkedHashSet<>(); for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
swap(arr, i, 0);
int start = 1;
int end = arr.length - 1;
while (start < end) {
if (arr[start] + arr[end] == sum - temp) {
int[] threeNum = {temp, arr[start], arr[end]};
Arrays.sort(threeNum);
result.add(new ThreeTuple<>(threeNum[0], threeNum[1], threeNum[2]));
start++;
end--;
} else if (arr[start] + arr[end] > sum - temp) {
end--;
} else {
start++;
}
}
swap(arr, i, 0); // 还原
}
System.out.println(result);
} // out: [-1:0:10, -1:2:8, -1:3:7, 0:2:7, 2:3:4] private void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

上面两题用到的元组类:

 class TwoTuple<A, B> {
public final A first;
public final B second; public TwoTuple(A a, B b) {
this.first = a;
this.second = b;
} @Override public String toString() {
return first + ":" + second;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((second == null) ? 0 : second.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TwoTuple<?, ?> other = (TwoTuple<?, ?>) obj;
if (first == null) {
if (other.first != null)
return false;
} else if (!first.equals(other.first))
return false;
if (second == null) {
if (other.second != null)
return false;
} else if (!second.equals(other.second))
return false;
return true;
}
}

TwoTuple<A, B>

 class ThreeTuple<A, B, C> extends TwoTuple<A, B> {
public final C third; public ThreeTuple(A a, B b, C c) {
super(a, b);
this.third = c;
} @Override public String toString() {
return super.toString() + ":" + third;
} @Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((third == null) ? 0 : third.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ThreeTuple<?, ?, ?> other = (ThreeTuple<?, ?, ?>) obj;
if (third == null) {
if (other.third != null)
return false;
} else if (!third.equals(other.third))
return false;
return true;
}
}

ThreeTuple<A, B, C>

题3、给定一个int正整数数组,一个数sum,求数组中和为sum的k个数的组合有多少种(k任意)。

另开一博客讨论这个问题。

指路:【算法习题】正整数数组中和为sum的任意个数的组合数——待完成

【算法习题】数组中任意2个(3个)数的和为sum的组合的更多相关文章

  1. 记录我对'我们有成熟的时间复杂度为O(n)的算法得到数组中任意第k大的数'的误解

    这篇博客记录我对剑指offer第2版"面试题39:数组中出现次数超过一半的数字"题解1的一句话的一个小误解,以及汇总一下涉及partition算法的相关题目. 在剑指offer第2 ...

  2. 1145: 零起点学算法52——数组中删数II

    1145: 零起点学算法52--数组中删数II Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 293 ...

  3. 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现

    原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...

  4. 从数组中任意取出2个数,判断他们的和是否为输入的数字sum,时间复杂度为0(n^2),空间复杂度0(1)

    从数组中任意取出2个数,判断他们的和是否为输入的数字sum,时间复杂度为0(n^2),空间复杂度0(1) 假设数据已经是排序好的 #include <stdio.h> #include & ...

  5. python实现给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果!

    题目描述:给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果! 样例: input:[["a","b"," ...

  6. 偶然在博客中见对百度一个面试题的探讨,写些自己的看法以及指出探讨中不对的观点:百度面试题:求绝对值最小的数 有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现 例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。

    今天申请了博客园账号,在下班后阅览博客时发现了一个关于百度面试题探讨的博客(其实是个很基础的问题),此博客url为:http://www.blogjava.net/nokiaguy/archive/2 ...

  7. [饭后算法系列] 数组中"和非负"的最长子数组

    1. 问题 给定一列数字数组 a[n], 求这个数组中最长的 "和>=0" 的子数组. (注: "子数组"表示下标必须是连续的. 另一个概念"子 ...

  8. 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  9. 编程算法 - 数字数组中只出现一次 代码(C)

    数字数组中只出现一次 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 一个整型数组里除了两个数字以外, 其它的数字都出现了两次. 请敲代码找出这 ...

随机推荐

  1. MongoDB的安装和使用

    Step1:下载和安装 下载地址:http://dl.mongodb.org/dl/win32/x86_64 安装:一直按照默认指示去安装或者选择自己喜欢的路径安装. Step2:配置环境变量 安装完 ...

  2. 解析HTTP报文——C#

    目前没有找到.Net框架内置的解析方法,理论上HttpClient等类在内部应该已经实现了解析,但不知为何没有公开这些处理方法.(亦或是我没找到)那么只能自己来解析这些数据了. public enum ...

  3. 局部变量,全局变量初始值问题----C与指针练习题4.14.1

    全局变量初始化0 局部变量初始化是随机值 如下面一段代码,全局变量,将src复制n个字符到dst #include<stdio.h> void copy_n(char dst[],char ...

  4. 线性求第k大

    快排变种. 快排每次只进行部分排序,进入左边或者右边或者当前mid就是答案. 据说期望值是O(n) 然后STL中的 nth_element也是用这个思想. #include <cstdio> ...

  5. JavaScript中易混淆的DOM属性及方法对比

    JavaScript中易混淆的DOM属性及方法对比 ParentNode.children VS Node.prototype.childNodes ParentNode.children:该属性继承 ...

  6. 装了anaconda之后如何设置anaconda、python环境变量

    装了anaconda之后如何设置anaconda.python环境变量 1.装了anaconda之后如何设置anaconda环境变量 参考 https://www.cnblogs.com/avivi/ ...

  7. python 第三方库

    1.tqdm 进度条 from tqdm import tqdm for i in tqdm(range(10000)): pass 2.fire 自动创建命令行接口(command line int ...

  8. Linux----------samba服务的安装使用及简介

      一.Samba简介 Samba是在linux和Unix系统上实现SMB协议的一个免费软件,由服务器端和客户端程序组成. Samba与nfs的不同,Samba比nfs多支持Windows SMB ( ...

  9. 03机器学习实战之决策树CART算法

    CART生成 CART假设决策树是二叉树,内部结点特征的取值为“是”和“否”,左分支是取值为“是”的分支,右分支是取值为“否”的分支.这样的决策树等价于递归地二分每个特征,将输入空间即特征空间划分为有 ...

  10. 环境准备阶段--搭建oracle linux 6.5系统

    环境准备阶段--搭建oracle linux 6.5系统 选择9.x版本兼容性 分配6GB内存 再次重启可以进入桌面,提示如下,勾选然后close 配置主机名,网络.hosts [root@ocp-1 ...