Second Max of Array

Posted YuriFLAG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Second Max of Array相关的知识,希望对你有一定的参考价值。

Find the second max number in a given array.

Example

Given [1, 3, 2, 4], return 3.

Given [1, 2], return 1.

Notice : Attention corner case. Communicate with Interviewer.

public class Solution {
    /**
     * @param nums: An integer array.
     * @return: The second max number in the array.
     */
    public int secondMax(int[] nums) {
       int max = Math.max(nums[0],nums[1]);
       int secondMax = Math.min(nums[0],nums[1]);
       for (int i = 2; i < nums.length; i++) {
               if (max <= nums[i]) {
                   secondMax = max;
                   max = Math.max(nums[i],max);
               }
       }
       return secondMax;
    }
}

 

以上是关于Second Max of Array的主要内容,如果未能解决你的问题,请参考以下文章

leetcode4 median Of two sorted array

LeetCode # Array # Easy # 697 Degree of an Array

1 代码片段1

LeetCode 702. Search in a Sorted Array of Unknown Size

TP5报如下的错误 Indirect modification of overloaded element of thinkpaginatorCollection has no effect(代码片段

Javascript 函数数组