I'm going to be unemployed Day 1 Sword Finger Offer 3. Repeated numbers in the array

Affected by the epidemic, it can only be a home. The atmosphere without a laboratory is completely a salted fish. The small papers are not restored to the teacher. Let's start the process of brushing the question of the sword offer today.
Find duplicate numbers in the array.

The title is: 3. Repeated numbers in the array

All numbers in an array nums of length n are in the range of 0 to n-1. Some numbers in the array are repeated, but I do not know how many numbers are repeated, nor how many times each number is repeated. Please find any duplicate number in the array.

Example 1:

Input:
[2, 3, 1, 0, 2, 5, 3]
Output: 2 or 3

class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        int a = 0;
        int flag = 0;
        int length;
        length = nums.size();
       
       sort(nums.begin(),nums.end());
        for(int i=0;i<length-1;i++)
        {   
            if (nums[i] == nums[i+1])
            {a = nums[i];}
        }
        return a;
    }
};

I want to vomit why the complexity cannot be n * n. Isn't the multi-layer loop fragrant? First of all, sorting is used. The complexity of bubble sorting is up, and then it times out. Use, although I do n’t know the principle, let ’s use it first. The begin () and end () written in the two parameters are automatically arranged, which is magical, and nums.size () can calculate the array elements. , I remember that the sizeof (nums) / sizeof (nums [0]) talked about by the dark horse programmer is not very easy to use.
Originally, the two-layer loop was used to solve the problem normally, but because of the time limit, only one layer of for loop can be used, and only the adjacent arrays can be sorted and compared ...
one chicken, TAT
time is too early, and then one liver ... hey, I do n’t know Can you find a job. sad

Published 1 original article · Liked 0 · Visits 2

Guess you like

Origin blog.csdn.net/qq_41227231/article/details/105656675