简单哈希表(课上)数组+线性

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]+" ");
}
}

}
posted @ 2024-02-11 23:04  赵千万  阅读(0)  评论(0编辑  收藏  举报