简单哈希表(课上)数组+线性
public class Main {
public static void main(String[] args) {
int[] nums = {2,4,6,2,3,4,6,7,8,9,0,1};
int []num1=new int[12];
for (int i = 0; i < num1.length; i++) {
num1[i]=Integer.MAX_VALUE;
}
int k=1,j;
for (int i = 0; i < nums.length; i++) {
if(num1[nums[i]%5]==Integer.MAX_VALUE)
{
num1[nums[i]%5]=nums[i];
}
else
{
j=1;
while(num1[(nums[i]+j)%12]!=Integer.MAX_VALUE)
{
j++;
}
num1[(nums[i]+j)%12]=nums[i];
}
}
for (int i = 0; i < 12; i++) {
System.out.print(num1[i]+" ");
}
}
}
public static void main(String[] args) {
int[] nums = {2,4,6,2,3,4,6,7,8,9,0,1};
int []num1=new int[12];
for (int i = 0; i < num1.length; i++) {
num1[i]=Integer.MAX_VALUE;
}
int k=1,j;
for (int i = 0; i < nums.length; i++) {
if(num1[nums[i]%5]==Integer.MAX_VALUE)
{
num1[nums[i]%5]=nums[i];
}
else
{
j=1;
while(num1[(nums[i]+j)%12]!=Integer.MAX_VALUE)
{
j++;
}
num1[(nums[i]+j)%12]=nums[i];
}
}
for (int i = 0; i < 12; i++) {
System.out.print(num1[i]+" ");
}
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17878543.html
千万千万赵千万