码迷,mamicode.com
首页 > 其他好文 > 详细

Second Max of Array

时间:2017-07-21 12:36:35      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:turn   solution   comm   strong   interview   amp   find   max   length   

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

标签:turn   solution   comm   strong   interview   amp   find   max   length   

原文地址:http://www.cnblogs.com/FLAGyuri/p/7216830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!