两数组用最少次数查出相同元素


package com;   

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class JudgeArrayContain {

static int[] M = new int[100];
static int[] N = new int[100000];
static HashMap m = new HashMap();
static int count = 0;
static List<Integer> result = new ArrayList<Integer>();
static long startDate;
static long endDate;

public static void main(String[] args) {
// 初始化数据
init();

// 输出M和N的长度
System.out.println("M的长度为=" + M.length);
System.out.println("N的长度为=" + N.length);

find(M, N);
// 试试哈希表
hash();

list();

besthash();
bestlist();

}

// 初始化数据
public static void init() {
for (int i = 0; i < 100; i++) {
M[i] = new Random().nextInt(100);
}
for (int i = 0; i < 100000; i++) {
N[i] = new Random().nextInt(100);
}
}

// 哈希算法
public static void hash() {
count = 0;
startDate = System.currentTimeMillis();
for (int i = 0; i < M.length; i++) {
m.put(i, M[i]);
count++;
}
for (int i = 0; i < N.length; i++) {
// 将N的数放到M哈希表的时候判断一下是否有一样的.
if (m.containsValue(N[i])) {
// System.out.println("N[" + i + "]=" + N[i]);
} else {
}
count++;
}
endDate = System.currentTimeMillis();
System.out.println("hash()循环了多少次count=" + count);
System.out.println("hash()执行时间:" + (endDate - startDate));
}

// best哈希算法
public static void besthash() {
count = 0;
if (M.length > N.length) {
startDate = System.currentTimeMillis();
for (int i = 0; i < M.length; i++) {
m.put(i, M[i]);
count++;
}
for (int i = 0; i < N.length; i++) {
// 将N的数放到M哈希表的时候判断一下是否有一样的.
if (m.containsValue(N[i])) {
// System.out.println("N[" + i + "]=" + N[i]);
} else {
}
count++;
}
endDate = System.currentTimeMillis();
System.out.println("besthash()循环了多少次count=" + count);
System.out.println("besthash()执行时间:" + (endDate - startDate));
} else {
startDate = System.currentTimeMillis();
for (int i = 0; i < N.length; i++) {
m.put(i, N[i]);
count++;
}
for (int i = 0; i < M.length; i++) {
// 将N的数放到M哈希表的时候判断一下是否有一样的.
if (m.containsValue(M[i])) {
// System.out.println("M[" + i + "]=" + M[i]);
} else {
}
count++;
}
endDate = System.currentTimeMillis();
System.out.println("besthash()循环了多少次count=" + count);
System.out.println("besthash()执行时间:" + (endDate - startDate));
}
}

// List算法
public static void list() {
count = 0;
startDate = System.currentTimeMillis();
for (int i = 0; i < M.length; i++) {
result.add(M[i]);
count++;
}
for (int i = 0; i < N.length; i++) {
// 将N的数放到M哈希表的时候判断一下是否有一样的.
if (result.contains(N[i])) {
// System.out.println("N[" + i + "]=" + N[i]);
} else {
}
count++;
}
endDate = System.currentTimeMillis();
System.out.println("list()循环了多少次count=" + count);
System.out.println("list()执行时间:" + (endDate - startDate));
}

// best哈希算法
public static void bestlist() {
count = 0;
if (M.length > N.length) {
startDate = System.currentTimeMillis();
for (int i = 0; i < M.length; i++) {
result.add(M[i]);
count++;
}
for (int i = 0; i < N.length; i++) {
// 将N的数放到M哈希表的时候判断一下是否有一样的.
if (result.contains(N[i])) {
// System.out.println("N[" + i + "]=" + N[i]);
} else {
}
count++;
}
endDate = System.currentTimeMillis();
System.out.println("list()循环了多少次count=" + count);
System.out.println("list()执行时间:" + (endDate - startDate));
} else {
startDate = System.currentTimeMillis();
for (int i = 0; i < N.length; i++) {
result.add(N[i]);
count++;
}
for (int i = 0; i < M.length; i++) {
// 将N的数放到M哈希表的时候判断一下是否有一样的.
if (result.contains(M[i])) {
// System.out.println("M[" + i + "]=" + M[i]);
} else {
}
count++;
}
endDate = System.currentTimeMillis();
System.out.println("bestlist()循环了多少次count=" + count);
System.out.println("bestlist()执行时间:" + (endDate - startDate));
}
}

// 先用最笨的方法
public static void find(int[] a, int[] b) {
count = 0;
startDate = System.currentTimeMillis();
for (int i = 0; i < M.length; i++) {
for (int j = 0; j < N.length; j++) {
if (M[i] == N[j]) {
// System.out.println(M[i]);
// break ;
}
count++;
}
}
endDate = System.currentTimeMillis();
System.out.println("find()循环了多少次count=" + count);
System.out.println("执行时间:" + (endDate - startDate));

}
}


来自http://www.iteye.com/problems/24897
二两集合差的常用用法
1
import java.util.ArrayList;   
import java.util.List;

/**
* 利用双重循环实现的筛选
*/
public class DoubleCycling{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};

// 筛选过程,注意其中异常的用途
List<String> ls=new ArrayList<String>();
for(String str:arr01){
try{
ls.add(getNotExistStr(str,arr02));
}
catch(Exception ex){
continue;
}
}

// 取得结果
Object[] arr03=ls.toArray();
for(Object str:arr03){
System.out.println(str);
}
}

/**
* 查找数组Arr中是否包含str,若包含抛出异常,否则将str返回
* @param str
* @param arr
* @return
* @throws Exception
*/
public static String getNotExistStr(String str,String[] arr) throws Exception{
for(String temp:arr){
if(temp.equals(str)){
throw new Exception("");
}
}

return str;
}
}

2速度较高的解法-利用哈希表

import java.util.ArrayList;   
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
* 利用哈希表进行筛选
*/
public class HashtableFilter{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};


Map<String,String> ht=new Hashtable<String,String>();

// 將arr02所有元素放入ht
for(String str:arr02){
ht.put(str, str);
}

// 取得在ht中不存在的arr01中的元素
List<String> ls=new ArrayList<String>();
for(String str:arr01){
if(ht.containsKey(str)==false){
ls.add(str);
}
}

// 取得结果
Object[] arr03=ls.toArray();
for(Object str:arr03){
System.out.println(str);
}
}
}

3最方便的解法-利用工具类


import java.util.ArrayList;
import java.util.List;

/**
* 使用工具类的筛选去除
*/
public class Tool{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};

// 直接转的话,生成的List不支持removeAll
List<String> ls01=new ArrayList<String>();
for(String str:arr01){
ls01.add(str);
}

// 同上
List<String> ls02=new ArrayList<String>();
for(String str:arr02){
ls02.add(str);
}

// 去除arr01中存在于arr02中的元素
ls01.removeAll(ls02);

// 取得结果
Object[] arr03=ls01.toArray();
for(Object str:arr03){
System.out.println(str);
}
}
}

4利用二叉树的解法

import java.util.ArrayList;   
import java.util.List;

/**
* 使用二叉樹的筛选去除
*/
public class Test{
public static void main(String[] args){
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};

// 以數組2為基礎創建二叉樹
Tree tree=new Tree();
for(String str:arr02){
tree.insert(str);
}

// 將在二叉樹中不存在的元素放入鏈錶
List<String> ls=new ArrayList<String>();
for(String str:arr01){
if(tree.find(str)==null){
ls.add(str);
}
}

// 輸出
for(String str:ls){
System.out.println(str);
}
}
}


5
Set s = new HashSet(arr01);
s.removeAll(arr02);
来自http://www.iteye.com/problems/19116
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值