zoukankan      html  css  js  c++  java
  • 剑指Offer(数组)-数组中重复的数字

      (数组中重复的数组)题目描述:

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

      解决办法一:利用哈希表

      关于方法的输入参数的说明,其中的numbers[]是输入的数组,length是其长度,duplication[]是待输出的数组。new 了一个HashSet之后,利用遍历的思想判断其中是否有每一个遍历的元素,如有就将duplication[0]的值赋值为numbers[i],然后返回true,结束当前的判断方法,因为找到了重复的数字。若没有找到就将此元素加入到HashSet中。需要注意的是import java.util.*这个包不能少,因为需要使用到Java自带的集合中的方法。

    import java.util.*;
    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) {
            Set<Integer> set = new HashSet<>();
            for(int i =0 ;i<length;i++){
                if(set.contains(numbers[i])){
                    duplication[0] = numbers[i];
                    return true;
                }else{
                    set.add(numbers[i]);
                }
            }
            return false;
        }
    }
    

      解决办法二:现将数组进行排序,然后再暴力查找相邻元素之间有没有重复的。先进行空判断。

    import java.util.*;
    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) {
        //考虑现将数组进行排序,排序之后再暴力查找相邻元素之间有咩有重复的
            if(numbers == null || length == 0){
                return false;
            }
            Arrays.sort(numbers);
            for(int i=0; i<length-1; i++){
                if(numbers[i] == numbers[i+1]){
                    duplication[0] = numbers[i];
                    return true;
                }
            }
            return false;
        }
    }
  • 相关阅读:
    关于Git的使用方法
    Python读取Excel数据
    用到的Dos命令总结 持续更新
    windows下使用Jenkins+Gitea持续集成
    HDFS概述(2)————Block块大小设置
    分布式文件系统比较出名的有HDFS  和 GFS
    c++里面有没有什么办法做到 判断某个给定的未知数是double类型还是int类型 呢?
    About HDFS blocks
    hadoop深入学习之SequenceFile
    使用RawComparator加速Hadoop程序
  • 原文地址:https://www.cnblogs.com/dashenaichicha/p/12518521.html
Copyright © 2011-2022 走看看