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. VS2010 VC++ 项目添加引用 出现 Internal CPS Error问题原因及解决办法

    在VS2010 VC++ 项目添加引用时,有时会出现Internal CPS Error错误,提示信息: 'Internal CPS Error: We couldn't find the exist ...

  2. SPSS19.0实战之多元线性回归

    线性回归数据来自于国泰安数据服务中心的经济研究数据库.网址:http://www.gtarsc.com/p/sq/.数据名称为:全国各地区能源消耗量与产量,该数据的年度标识为2006年,地区包括我国3 ...

  3. QT在windows下的安装与配置

    先了解Qt: Qt一直以来,分为商业.开源两个版本,商业版本为用户提供了二级制的动态库,直接安装既可以使用,但是需要花钱购买license,而开源版本则遵守GPL协议,提供了源码,用户需要自行编译,才 ...

  4. 异类的Javascript处理和解析URL的方式

    通常来说,我们使用Javascript处理和解析URL是使用location对象.在今天这个代码小技巧中,我们使用另外一个比较异类的方式处理和解析URL. 代码如下: function parseUR ...

  5. HAWQ技术解析(八) —— 大表分区

    一.HAWQ中的分区表        与大多数关系数据库一样,HAWQ也支持分区表.这里所说的分区表是指HAWQ的内部分区表,外部分区表在后面"外部数据"篇讨论. 在数据仓库应用中 ...

  6. 【Caffe代码解析】Layer网络层

    Layer 功能: 是全部的网络层的基类,当中.定义了一些通用的接口,比方前馈.反馈.reshape,setup等. #ifndef CAFFE_LAYER_H_ #define CAFFE_LAYE ...

  7. UNIX网络编程读书笔记:基本SCTP套接口编程

    概述 SCTP是一个较新的传输协议,于2000年在IETF得到标准化(TCP是在1981年标准化的).它最初是为满足不断增长的IP电话市场设计的:具体地说,就是穿越因特网传输电话信令. SCTP是一个 ...

  8. daemon与服务(service)及重启服务的方法

    简单地说,系统为了某些功能必须要提供一些服务(不论是系统本身还是网络方面),这个服务就称为service.而实现这个service的程序我们就称它为daemon.实现某个服务是需要一个daemon在后 ...

  9. poj 1125 (floyed 最短路径)

    Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26395   Accepted: ...

  10. 零java基础搞定微信Server

    自从打通了微信client到BLE设备端的通道.我的工作就转移到了server端了.微信的BlueDemoServer是以java编写,而本尊仅仅懂得嵌入式,对JavaWeb一窍不通. 本尊贵为IOT ...