【算法习题】数组中任意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 题目: 一个整型数组里除了两个数字以外, 其它的数字都出现了两次. 请敲代码找出这 ...
随机推荐
- 浅谈ES6新增数据类型:Symbol
面试中喜闻乐见的问题就是问我们的ES6新增了哪些个新特性 这篇文章一起学习一下新增的数据类型:Symbol JS的原始数据类型:6种Boolean,String,Undefined,NULL,Numb ...
- Python第九课学习
Python第九课学习 数据结构: 深浅拷贝 集合set 函数: 概念 创建 参数 return 定义域 www.cnblogs.com/yuanchenqi/articles/5782764.htm ...
- 微信小程序上传文件遇到的坑
在开发小程序时,使用的花生壳做的内网映射,域名使用花生壳卖的https域名 在做小程序文件上传时,调用接口,老是报错. Caused by: org.apache.commons.fileupload ...
- 新增和编辑clob字段
#region 新的数据新增和修改方法 /// <summary> /// 添加信息 /// </summary> /// <returns></return ...
- java clone()
Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象.所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象 ...
- leetcode 222.Count Complete Tree Nodes
完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...
- vue 项目搭建步骤
环境搭建步骤: 打开git ,运行 npm install --global vue-cli 这是安装vue的命令行 vue init webpack vue-demo 这是vue基于webpack的 ...
- Number.toLocalString() js
地址链接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocal ...
- Java第2次作业
我认为这一次的作业还是比较好的,对自己的学习有很大帮助.
- hello1实例的分析
JSF简介一. 什么是 JSF:JavaServer Faces (JSF) 是一种用于构建 Web 应用程序的新标准 Java 框架.它提供了一种以组件为中心来开发 Java Web 用户界面的方法 ...