【算法习题】数组中任意2个(3个)数的和为sum的组合
题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的组合的更多相关文章
- 记录我对'我们有成熟的时间复杂度为O(n)的算法得到数组中任意第k大的数'的误解
这篇博客记录我对剑指offer第2版"面试题39:数组中出现次数超过一半的数字"题解1的一句话的一个小误解,以及汇总一下涉及partition算法的相关题目. 在剑指offer第2 ...
- 1145: 零起点学算法52——数组中删数II
1145: 零起点学算法52--数组中删数II Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 293 ...
- 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...
- 从数组中任意取出2个数,判断他们的和是否为输入的数字sum,时间复杂度为0(n^2),空间复杂度0(1)
从数组中任意取出2个数,判断他们的和是否为输入的数字sum,时间复杂度为0(n^2),空间复杂度0(1) 假设数据已经是排序好的 #include <stdio.h> #include & ...
- python实现给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果!
题目描述:给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果! 样例: input:[["a","b"," ...
- 偶然在博客中见对百度一个面试题的探讨,写些自己的看法以及指出探讨中不对的观点:百度面试题:求绝对值最小的数 有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现 例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。
今天申请了博客园账号,在下班后阅览博客时发现了一个关于百度面试题探讨的博客(其实是个很基础的问题),此博客url为:http://www.blogjava.net/nokiaguy/archive/2 ...
- [饭后算法系列] 数组中"和非负"的最长子数组
1. 问题 给定一列数字数组 a[n], 求这个数组中最长的 "和>=0" 的子数组. (注: "子数组"表示下标必须是连续的. 另一个概念"子 ...
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 编程算法 - 数字数组中只出现一次 代码(C)
数字数组中只出现一次 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 一个整型数组里除了两个数字以外, 其它的数字都出现了两次. 请敲代码找出这 ...
随机推荐
- redis客户端(三)
redis客户端 一.>redis自带的客户端 启动 启动客户端命令:[root@ming bin]# ./redis-cli -h xxx.xxx.xx.xxx-p 6379 注意: -h:指 ...
- [转]CentOS7利用systemctl添加自定义系统服务
原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...
- 如何通过Chrome远程调试android设备上的Web网站
网上的帖子很多,但很多都是老版本的,试过了,根本不管用,花了一天时间,终于在本机试验通过了,特记录下来,以备用.有需要的朋友也可以参考.先上一张图,看看PC端chrome上调试的效果: 左边是手机的模 ...
- 虚拟机VMware显示“内部错误”的解决方法
很有可能是VM服务没有启动,win+R services.msc 进入 “服务”,将VM相关的5个服务全部启动即可.
- linux学习笔记2 - linux常用命令
转载请标注原链接:http://www.cnblogs.com/xczyd/p/5543731.html 第一篇博客:linux学习笔记1-ubuntu的安装与基本设置 之中,已经介绍了如何安装lin ...
- Multiple plot function
From: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ library(ggplot2) multi ...
- flask中需要的基本配置信息
1.秘钥设置: app.secret_key = '随意设置' 2.SQLALCHEMY配置: # 连接数据库 app.config['SQLALCHEMY_DATABASE_URI'] = 'mys ...
- SQLI DUMB SERIES-17
(1)无论怎么输入username,都没有回显.尝试改变password的输入.找到了闭合方式:单引号 (2)报错注入: 爆库名 admin&passwd=admin' and extract ...
- JAVA第九次作业
JAVA第九次作业 (一)学习总结 1.用思维导图对javaIO操作的学习内容进行总结. 参考资料: XMind. 2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低.使用 ...
- 不同系统、不同存储格式(textfile, parquet)数据的传递
描述: 本地测试环境hive中有数据,存储格式为textfile,现在要上传到公司开发环境,存储格式为parquet, 如何实现??? tb_textfile表---> local file - ...