题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. 2018-软工机试-C-和你在一起

    C. 和你在一起 单点时限: 1.0 sec 内存限制: 256 MB 我想和你在一起 直到我不爱你 宝贝 人和人 一场游戏 我愿意为你死去 如果我还爱你 宝贝 反正活着 也没意义 宝贝 我也只能 这 ...

  2. Javascript 严格模式(strict mode)详解

    Javascript 严格模式详解   一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Ja ...

  3. 天转凉了,注意保暖,好吗(需求规格说明书放在github了)

    团队项目——AI五子棋(小程序) 一.团队展示: 队名:未来的将来的明天在那里等你 小组 队员: 龙天尧(队长)(3116005190),林毓植(3116005188),黄晖朝(3116005178) ...

  4. nginx配置https转发http

    生成ssl证书: 1.首先要生成服务器端的私钥,运行时会提示输入密码,此密码用于加密key文件: openssl genrsa -des3 -out server.key 1024 2.去除key文件 ...

  5. python-markdown

    python-markdown无法将“`生成标签问题解决方法 2种都是代码区块 ```swift is Int ``` is Int

  6. vue 安装

    安装cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 安装 vue.js cnpm install -g vue- ...

  7. oracle数据库连接缓慢

    今天连接数据库时非常的慢,大概将近1分钟,才能连上,感觉不对,登录数据库所在的服务器,进行检查,常规的内存.硬盘.CPU全都正常,然后想要进入oracle用户时报错了:su: cannot set u ...

  8. DataTables中自增序号的实现

    最近工作中,一直在写前端的东西,一直以来前端都接触的少,所以侧重点也没放在这边,导致现在工作进展比较慢,果然偷懒是要不得的啊.... 今天刚写了一个在DataTables中增加一列自增序号,刚开始以为 ...

  9. Matlab 将RGB 图像转换成YCrCb图像

    >> im = imread('trees.jpg');>> imshow(im)>> ycrcb_trees = rgb2ycbcr(im);>> f ...

  10. PAT乙级考前总结(一)

    数学相关的题目 1001 害死人不偿命的(3n+1)猜想 (15 分) 直接一步步计数 1005 继续(3n+1)猜想 (25 分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目 ...