leetcode: 414. The third largest number (8 ms 8.7 MB)

leetcode: 414. The third largest number

Topic:
Give you a non-empty array and return the third largest number in this array. If it does not exist, the largest number in the array is returned.

Example 1:

Input: [3, 2, 1] Output: 1 Explanation: The third largest number is 1.

Example 2:

Input: [1, 2] Output: 2 Explanation: The third largest number does not exist, so the largest number 2 is returned.

Example 3:

Input: [2, 2, 3, 1] Output: 1

Explanation: Note that the requirement to return the third largest number refers to the third largest and unique number. There are two numbers with a value of 2, and they both rank second.

prompt:

1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1

Step by step analysis:

  • The first step to get my hands, I think about sorting, but ignore a problem: if you input 1111. or 2223331111, the output nums [2] is not the third largest number. So this idea is not ideal
  • Then I looked at the analysis of Java and analyzed as follows:
  • Set three numbers, one, two, three, traverse the array;
  • When the input number is greater than one, let three = two; two = one; then one is replaced with the input number;
  • When the input number is between one and two, one does not change, three becomes two, and two becomes the input number;
  • When the input number is between two and three, both one and two are unchanged, and the input number is replaced with the original three;
  • Note: When the number entered is the same as one or two or three, we do not change the value of the original three numbers, but just let the program continue to run, without exiting the for loop, but ending the if statement (using continue)

but (I think the more important new knowledge is coming)

  • When running, we will find a problem, the system has entered a very large number, which has exceeded the range of int. But at this time, if the range is fixed (use the if statement to determine the size of the input number), there will be an operation without a return value, which does not conform to the standard mode of leetcode
  • Solution:
  • When we assign one, two, and three to the minimum value in the data structure long, this error can be avoided
  • Here we need to understand how the maximum and minimum values ​​of data types in various languages ​​are assigned. I will only introduce the assignment method in C++ here.

Specific link:
[Link] How to get the maximum and minimum values ​​of various data types in C++

class Solution {
    
    
public:
    int thirdMax(vector<int>& nums) {
    
    
        int n = nums.size();
       //特殊:
        if(n == 1)
            return nums[0];

        if(n == 2)
            return nums[0]>nums[1] ? nums[0] : nums[1];
        //将其赋值为long型的最小数字
        long one =LONG_MIN;
        long two = LONG_MIN;
        long three = LONG_MIN;
        //遍历数组
        for(int i = 0;i < n;i++){
    
    

                if(nums[i] == one||nums[i] == two)
                    continue;
                    //输入数字大于one
                if(nums[i] > one){
    
    
                    three = two;
                    two = one;
                    one = nums[i];
                }else 
                //输入数字小于one,大于two
                if(nums[i] > two){
    
    
                    three = two;
                    two = nums[i];
                }else 
                //输入数字大于three,小于two
                if(nums[i] > three){
    
    
                    three = nums[i];
                }       
     }
     //特例输入,比如11113333,此时输出应该时最大值,为one
        return (three == LONG_MIN) ? one : three;
    
    }
};

Guess you like

Origin blog.csdn.net/weixin_42198265/article/details/114587586