java面试(九)--(1)List和ArrayList的区别,arrayList和HashSet区别(2)桶排序问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/JAVA_I_want/article/details/102580644

1. List和ArrayList的区别,arrayList和HashSet区别

list是接口,arraylist是实现该接口的类,arraylist底层是动态数组,有序且可以重复hashset底层是hash表,由于会比较hash值和equals,所以他是不可重复并且是无序的。简单的说一个是有序可重复,一个是无序不可重复。

2.桶排序问题

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

示例代码:

public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
     
    }
}


实现代码:

public class TestBucketSort {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false

    public boolean duplicate(int numbers[],int length,int [] duplication) {
        //存储0-9数字的位置,出现一次对应位置则为true,出现2次即以上,则为重复数字
        boolean[] saveIndex=new boolean[10];
        //如果数组长度为0,返回false
        if(length==0) return false;
        //duplication下标位置
        int flag=0;
        for(int i=0;i<length;i++){
            if(i==0){
                saveIndex[numbers[i]]=true;
            }else{
                if(!saveIndex[numbers[i]]){//数字未出现过
                    saveIndex[numbers[i]]=true;
                }else{
                    //数字出现过,duplication里已存储
                    for(int j=0;j<duplication.length;j++){
                        if (numbers[i]==duplication[j]) continue;
                    }
                    //数字出现过,duplication里未存储
                    duplication[flag++]=numbers[i];
                }
            }
        }
        if(duplication.length>0){
            for (int i=0;i<duplication.length;i++){
                System.out.println(duplication[i]);
            }
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        TestBucketSort test=new TestBucketSort();
        int[] numbers=new int[]{2,5,7,4,2,5};
        int [] duplication=new int[numbers.length];
        boolean duplicate = test.duplicate(numbers, numbers.length, duplication);
        System.out.println(duplicate);
    }

}

如果有不知道桶排序的读者,可以看看下面的这篇博文:
桶排序解析:https://www.cnblogs.com/bqwzx/p/11029264.html

猜你喜欢

转载自blog.csdn.net/JAVA_I_want/article/details/102580644