Find the second max number in a given array.

Notice

You can assume the array contains at least two numbers.

 
Example

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

Given [1, 2], return 1.

解法一:

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

从数组前两位找到first大和second大的,从i=2开始遍历,不断找当前first大和second大。

479. Second Max of Array【easy】的更多相关文章

  1. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  2. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  3. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  4. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  5. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  6. 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  7. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  8. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  9. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

随机推荐

  1. 升级struts到2.5.2遇到的问题及解决方案

    原来的版本是2.3.x,由于安全原因需要升级到2.5.2.1,2.5.2版本不再提供xwork.jar ,整合到了 struts-core包中. 2,方法不能访问的问题,需要在每个action配置文件 ...

  2. 使用R语言和XML包抓取网页数据-Scraping data from web pages in R with XML package

    In the last years a lot of data has been released publicly in different formats, but sometimes the d ...

  3. UDP套接字——(DGRAM)

    /*********************程序相关信息********************* * 程序编号:014 * 程序编写起始日期:2013.11.29 * 程序编写完成日期:2013.1 ...

  4. JavaScript hash

    location.hash -- 返回URL#后面的内容,如果没有#,返回空 hash,中文"哈希" 引用网址:http://www.dreamdu.com/javascript/ ...

  5. 一种Android数据请求框架

    大部分Android应用一般都涉及到跟server的交互,除非是某些单机应用.既然要跟server打交道,向server请求数据差点儿是必做的事情,或许每家的APP都有一套自己的详细实现逻辑.但我们还 ...

  6. kCGImagePropertyExifDictionary 引用错误

    kCGImagePropertyExifDictionary 引用错误 使用 AVFoundation拍照 //获取图片 [outputImage captureStillImageAsynchron ...

  7. Linux下全局符号覆盖问题

    在windows上,默认情况下,动态库中的符号都是对外隐藏的,除非你显示的指出要导出哪些符号,否则外界是看不到的.但是linux下情况刚好相反,对静态变量和全局变量,linux下so里面的符号对外可见 ...

  8. 转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes)

    转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl ...

  9. vue 表单 验证 async-validator

    1.使用插件async-validator async-validator 地址:https://github.com/yiminghe/async-validator 2.示例(vue+elemen ...

  10. 查看sqlserver 2008中性能低下的语句

    经常使用这个语句来查看性能低下的sql语句: SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total_physical ...