479. Second Max of Array【easy】
Find the second max number in a given array.
Notice
You can assume the array contains at least two numbers.
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】的更多相关文章
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 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 ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 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 ...
- 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 ...
随机推荐
- Labeled Faces in the Wild 人脸识别数据集 部分测试数据
development test set Note: images displayed are original (non-aligned/funneled) images. match pairs ...
- go语言基础之类型别名
1.类型别名 示例: package main //必须有一个main包 import "fmt" func main() { //给int64起一个别名叫bigint type ...
- CSS 中的强制换行和禁止换行
强制换行 1.word-break: break-all; 只对英文起作用,以字母作为换行依据. 2.word-wrap: break-word; 只对英文起作 ...
- CentOS7下命令安装火狐浏览器
使用命令安装火狐浏览器,需要切换root(su root)下,执行下面的命令,自动下载所需依赖包,完成安装 yum -y install firefox 然后重启即可
- 多个Mapper和Reducer的Job
多个Mapper和Reducer的Job @(Hadoop) 对于复杂的mr任务来说,只有一个map和reduce往往是不能够满足任务需求的,有可能是需要n个map之后进行reduce,reduce之 ...
- python获取系统时间代码解析
import time print time.time() 输出的结果是: 1279578704.6725271 但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块 ...
- [笔记][Java7并发编程实战手冊]3.8 并发任务间的数据交换Exchanger
[笔记][Java7并发编程实战手冊]系列文件夹 简单介绍 Exchanger 是一个同步辅助类.用于两个并发线程之间在一个同步点进行数据交换. 同意两个线程在某一个点进行数据交换. 本章exchan ...
- UML 之 用例图
用例图是指由参与者(Actor).用例(Use Case)以及它们之间的关系构成的用于描述系统功能的静态视图.用例图(User Case)是被称为参与者的外部用户所能观察到的系统功能的模型图,呈现了一 ...
- pomelo生命周期回调和组件加入
一 生命周期回调 生命周期回调可以让开发人员在不同类型的server生命周期中进行详细操作. 提供的生命周期回调函数包含:beforeStartup,afterStartup,beforeShutdo ...
- 【BIRT】修改BIRT的背景颜色
修改BIRT报表的背景颜色都在这里了 在BIRT的webcontent/birt/styles/目录下有如下文件列表: dialogbase.css文件修改 dialogbase_rtl.css文件修 ...