package com.lance.algorithm.算法;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Sanlie {
private static Sanlie instance = new Sanlie();
private Sanlie() {
};
public static Sanlie getInstance() {
return instance;
}
public static void main(String[] args) {
Sanlie instance = Sanlie.getInstance();
String[] str1 = new String[] { "11", "3", "3", "3" };
String[] str2 = new String[] { "3" };
List<Result> result = instance.doSanlie(str1, str2);
// List<Result> result = instance.doSanlie(str1);
for (Result resultValue : result) {
System.out.println(resultValue.getIndex() + " "
+ resultValue.getValue());
}
}
/**
* 获取两个数组中相同的字符
*
* @param str1
* str1
* @param str2
* str2
* @return List<String>
*/
public List<Result> doSanlie(String[] str1, String[] str2) {
List<Result> result = new ArrayList<Result>();
int max = str1.length > str2.length ? str1.length : str2.length;
int min = str1.length < str2.length ? str1.length : str2.length;
int[][] cache = new int[max * min][];
for (String str1Value : str1) {
int hash = hash(str1Value.hashCode());
int index = index(hash, max * min);
if (cache[index] == null) {
cache[index] = new int[1];
} else {
cache[index] = Arrays.copyOf(cache[index],
cache[index].length + 1);
}
for (int j = 0; j < cache[index].length; j++) {
if (cache[index][j] == 0) {
cache[index][j] = hash;
break;
}
}
}
java 散列算法计算重复数据
转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java 散列分区 java散列算法
文章部分代码图片和总结来自参考资料哈希和常用的方法 散列,从中文字面意思就很好理解了,分散排列,我们知道数组地址空间连续,查找快,增删慢,而链表,查找慢,增删快,两者结合起来形成散列表。如下图。
java 散列分区 java 数据结构与算法 取模 链表