[Sword pointing to offer] Interview question 03: Repeated numbers in an array

[Sword pointing to offer] Interview question 03: Repeated numbers in an array

Topic description

All numbers in an array of length n are in the range 0 to n-1. Some numbers in the array are repeated, but I don't know how many numbers are repeated. Also don't know how many times each number is repeated. Find any repeated number in the array. For example, if the input array {2,3,1,0,2,5,3} of length 7, then the corresponding output is the first repeated number 2.

problem solving ideas 1

  • The most straightforward idea: create an array nums to count the number of occurrences of each number in the input array;
    • First traverse the input array number, update the value of nums, and the value of nums[i] represents the number of times the number i appears in the input array;
    • Traverse nums again, assign the value of nums[i] greater than or equal to 2 to duplication[0], and return true
    • Only two traversals are required, time complexity O(n), space complexityO(n)
public class Solution {
    //number 是输入的数组,length 是数组长度
    //duplication 是长度为1的数组,duplication[0]保存的是任意一个重复的元素
    public boolean duplicate(int number[],int length,int [] duplication) {
        if(number == null || length == 1){
            return false;
        }
        int[] nums = new int[length];
        for(int i =0; i< length;i++){
            nums[number[i]]++;
        }
        for(int i =0;i<length;i++){
            if(nums[i] >=2){
                duplication[0] = i;
                return true;
            }
        }
        return false;
    }

    //变种
    public boolean duplicate(int number[],int length,int [] duplication) {
        if(number == null || length == 1){
            return false;
        }
        int[] nums = new int[length];
        for(int i =0;i<length;i++){
            if(nums[number[i]] ==1){
                duplication[0] = number[i];
                return true;
            }
            nums[number[i]] =1;
        }
        return false;
    }
}

Problem solving idea 2

  • Use the characteristics of the data itself: an array of length n, the data range is [0~n-1], arrange the array in ascending order, and the repeated numbers will only be close together; therefore, traverse the sorted array, compare nums[ i] and nums[i+1]; if they are equal, it means repetition, time complexity O(n+nlogn), space complexityO(1)
import java.util.Arrays;
public class Solution {
    //number 是输入的数组,length 是数组长度
    //duplication 是长度为1的数组,duplication[0]保存的是任意一个重复的元素
    public boolean duplicate(int number[],int length,int [] duplication) {
        if(number == null || length == 1){
            return false;
        }
        Arrays.sort(number);
        for(int i =0;i<length-1;i++){
            if(number[i] == number[i+1]){
                duplication[0] = number[i];
                return true;
            }
        }
        return false;
    }
}

Problem solving idea 3

  • The solution provided on "Swordsman Offer"
  • Personal understanding: If each number in the input array is non-repetitive, then if the input array number is sorted, it should satisfy number[i] == i.
  • Then in order to achieve the above state, now for the number of the initial state:
    • If number[i] == i, continue to traverse backwards;
    • If number[i] == m, compare the values ​​of number[i] and number[m], if they are equal, it means repeat, otherwise swap the values ​​of number[i] and number[m], and put m in it where it should appear until number[i] == i.
public class Solution {
    //number 是输入的数组,length 是数组长度
    //duplication 是长度为1的数组,duplication[0]保存的是任意一个重复的元素
    public boolean duplicate(int number[],int length,int [] duplication) {
        if(number == null || length == 1){
            return false;
        }
        for(int i =0;i<length-1;i++){
            while(number[i] != i){
                if(number[i] == number[number[i]]){
                    duplication[0] = number[i];
                    return true;
                }
                int temp = number[i];
                number[i] = number[number[i]];
                number[number[i]] = temp;
            }
        }
        return false;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325993748&siteId=291194637