nowcoder NC30 missing first positive integer

Table of contents

Topic description:

analyze:

Complete code:


 

Question link:  icon-default.png?t=N7T8https://www.nowcoder.com/share/jump/819478881694767416272

Topic description:

Given an integer array nums without repeated elements, please find the smallest positive integer that does not appear in it.

Advancedspace complexity O(1), time complexity O(n)

Example 1

Input: [1,0,2]

Return value: 3

Example 2

Input: [-2,3,4,1,5]

Return value: 2

Example 3

Input: [4,5,6,8,9]

Return value: 1

analyze:

After reading the question and the three examples given, we can draw the following conclusions: 

  1. The data in the given array are random integers and unordered;
  2. The data in the given array contains negative numbers;
  3. The return value must be no less than 1.

The space complexity required by the question is O(1) and the time complexity is O(n), so we should try to reduce the space opening as much as possible.

This question can use a hash table, but the space complexity is O(n), so this method should be used only if you really can't think of a solution.

So at this time we can start from the conclusion drawn above . Since the array is unordered, we can first use the library method to sort it.

    public int minNumberDisappeared (int[] nums) {
        Arrays.sort(nums);        
    }

After sorting, our ideas immediately opened up. Since there may be numbers no greater than 0 in the array, we can first use a loop to skip this part :

    public int minNumberDisappeared (int[] nums) {
        int ret = 0; //返回值
        int len = nums.length; //数组长度
        Arrays.sort(nums);
        int i = 0; //记录数组遍历的位置

        while (i < len && nums[i] <= 0) {
            i++;
        }
    }

At this time, there are only numbers left in the array that are not less than 0. It is very simple to make a judgment at this time. We can directly judge whether there is an integer between the two numbers and return the integer if it exists. If the array is [1,2,3,4] then we return the last number plus one.

        while (i < len - 1) {
            if (nums[i] + 1 < nums[i+1]) {
                return nums[i] + 1;
            }
            i++;
        }
        ret = nums[len-1] + 1;
        return ret;

Finally, we come to the step that is essential every time we do the questions: limit value judgment.

Then judge the array at the beginning of the code: if the array does not exist, return the smallest positive integer 1 .

        if (nums == null || nums.length == 0) {
            return 1;
        }

After the first loop, it is judged that if there is no 1 in the array, then 1 is returned .

        if (nums[i] - 1 >= 1) {
            return 1;
        }

 At this time, if the complexity of the library function is ignored, the complexity we use in this question is: space complexity O(1), time complexity O(n).

 

Complete code:

import java.util.*;


public class Solution {

    public int minNumberDisappeared (int[] nums) {
        // write code here
        if (nums == null || nums.length == 0) {
            return 1;
        }
        int ret = 0;
        int len = nums.length;
        Arrays.sort(nums);
        int i = 0;

        while (i < len && nums[i] <= 0) {
            i++;
        }
        if (nums[i] - 1 >= 1) {
            return 1;
        }
        while (i < len - 1) {
            if (nums[i] + 1 < nums[i+1]) {
                return nums[i] + 1;
            }
            i++;
        }
        ret = nums[len-1] + 1;
        return ret;
    }
}

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/132908458