cyc2018剑指offer刷题-3.数组重复数字问题

附:java常见STL

3. 数组中重复的数字

题目描述

在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次。请找出数组中任意一个重复的数字。

双重循环比较就太粗糙了时间复杂度O(n2),不做讨论

extend:C++保险判别空指针

首先NULL和nullptr都能代表空指针,但是在C++中代表的含义是不同的,NULL被定义为整型变量0或者直接就是由0转化成的void*。NULL直接默认传入的是整型变量,所以在C++中使用nullptr相对是比较保险的。
int *p;
if(p==nullptr)

1.初始菜鸟思路基于排序

我刚开始想过先排序再遍历比较相邻元素的值,空间复杂度O(1),时间复杂度取决于你使用的排序算法
空间复杂度:是对一个算法在运行过程中临时占用得存储空间大小的量度,一般也作为问题规模n得函数,以数量级形式给出,记作:S(n) = O(g(n))
我直接使用库函数进行排序 :C++ 中的sort()排序函数用法

//C++
#include<algorithm>
using namespace std;
class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        bool flag=false;
        if(length<2)
            return flag;
        else {
            sort(numbers,numbers+length);
            for(int i=0;i<length-1;i++){
                if(numbers[i]==numbers[i+1])
                {
                    duplication[0]=numbers[i];
                    flag=true;
                    break;
                }
                
            }
            return flag;
        }
        
    }
};
//JAVA

import java.util.*;
public class Solution {
    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;
    }
}

2.空间换时间

链接:https://www.nowcoder.com/questionTerminal/623a5ac0ea5b4e5f95552655361ae0a8?answerType=1&f=discussion
来源:牛客网
//哈希表
import java.util.*;
public class Solution {
    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;
    }
}
链接:https://www.nowcoder.com/questionTerminal/623a5ac0ea5b4e5f95552655361ae0a8?answerType=1&f=discussion
来源:牛客网
//  定义辅助数组记录数字出现次数
class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        if(numbers==NULL || length<1)
            return false;
        int* array=new int[length]();//构建hash表,初始化为0
        for(int i=0;i<length;i++){
            ++array[numbers[i]];//存储每一个数出现的次数,
            if(array[numbers[i]]>1){//出现的次数大于1,即第一个重复的数字
                *duplication=numbers[i];
                return true;
            }
        }
        return false;
        
    }
};
import java.util.HashSet;
import java.util.Set;
public class Solution {
    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;
    }
}
//使用辅助数组的写法
public class Solution {
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        int[] arr = new int[length];
        for(int j = 0;j<length;j++){
            arr[j] = -1;  //辅助数组赋初值,只要不在1到n-1即可,否则在后续判断时会出错
        }
        for(int i =0 ;i<length;i++){
            if(arr[numbers[i]] == numbers[i]){
                duplication[0] = numbers[i];
                return true;
            }else{
                arr[numbers[i]] = numbers[i];
            }
        }
        return false;
    }
}
//显然用来记录次数的辅助数组,不用赋初值  只要次数超过1就可以返回了,++的条件时次数为零
public class Solution {
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        int[] arr = new int [length];
        for(int i = 0; i < length; i++){
            if(arr[numbers[i]] == 0){
                arr[numbers[i]] ++;
            }else{
                duplication[0] = numbers[i];
                return true;
            }
        }
        return false;
    }
}


 //hashmap方法
 public class Solution {
      Map<Integer,Integer> map = new HashMap<Integer,Integer>();
      boolean flag = false;
      for(int i=0;i<length;i++){
          if(!map.containsKey(numbers[i]))map.put(numbers[i],1);
          else{
              flag = true;
              duplication[0]=numbers[i];
              break;
          }
      }
      return flag;
    }

3.利用题目独有的特性

长度n,元素大小却在0-n-1,因此如果每个数字都不重复时,排完序的数组中的元素值和该元素的索引相同,而一旦有重复的数,那就会出现,元素和角标不相同的情况,基于这个条件,构建思路4,也叫重排数组(其实和哈希表想法相似,当同一个位置元素出现多次时,说明重复了)这个想法需要不断交换对应的元素。比如{4,1,3,1,4} ,将索引为0的4移动到索引为4的元素交换,让4归位,但是移动时发现索引为4的元素已经是4了,就说明出现了两次。

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    void swap(int &a,int &b)
    {
        int temp;
        temp=a;
        a=b;
        b=temp;
    }
    bool duplicate(int numbers[], int length, int* duplication) {
        
        if(numbers==nullptr||length<2){
            return false;
        }
        else
        {    
            for(int i=0;i<length;i++)
            {   //遇到下标相等的不需要理会,位置无需变动
              while (numbers[i] != i)   //不和下标相等才有比较意义
              {  //有重复必然出现一个数和下标不等
                    if (numbers[i] == numbers[numbers[i]]) // 比较该处值和它应该在的索引位置的值
                    { //相等则说明重复
                        duplication[0] = numbers[i];
                        return true;
                    }
                        swap(numbers[i] , numbers[numbers[i]]);//将该数放在下标相等处
                }
            }
            
        }
        return false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值