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 ...
随机推荐
- DTO的一些理解(转载)
1.什么是DTO DTO(Data Tansfer Object)即数据传输对象.之前不明白有些框架中为什么要专门定义DTO来绑定表现层中的数据,为什么不能直接用实体模型呢,有了DTO同时还要维护DT ...
- 如何判断linux使用的是HDD还是SSD、HHD;磁盘阵列RAID
硬盘种类:SSD固态硬盘.HDD机械硬盘.HHD混合硬盘(里面既有机械硬盘也有固态硬盘,固态硬盘用来高速缓存,机械硬盘用来存储). HHD是机械硬盘和固态硬盘的结合体.我们可以理解为其就是两块硬盘,固 ...
- Java:Object类详解
Java的一些特性会让初学者感到困惑,但在有经验的开发者眼中,却是合情合理的.例如,新手可能不会理解Object类.这篇文章分成三个部分讲跟Object类及其方法有关的问题. 上帝类 问:什么是Obj ...
- 了解Redis 和 Memcached 的区别
1.Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支持更丰富的数据操作,通常在Memcached里,你需要将数据拿到客户端来进行类似的修改再set回去. ...
- 【转】javascript 中的很多有用的东西
原文:https://www.cnblogs.com/ys-ys/p/5158510.html ---------------------------------------------------- ...
- Unity5.1 新的网络引擎UNET(十五) Networking 引用--下
孙广东 2015.7.21 本节提供了与网络系统一起使用的组件的具体信息. 10.Network Proximity Checker Suggest a change Success! Than ...
- JavaScript中isPrototypeOf函数
转自:http://www.ijavascript.cn/shouce/javascript-isprototypeof-247.html JavaScript中 isPrototypeOf 函数方法 ...
- 锁Lock的概念
并发访问同一资源的功能,需要引入锁Lock的概念,也就是说,某个时刻,当有一个访问类访问这个数据对象时,这个数据对象必须上锁Locked,用完后就立即解锁unLocked,再供其它访问类访问.
- UNIX网络编程调试工具:tcpdump、netstat和lsof
tcpdump程序 tcpdump一边从网络读入分组一边显示关于这些分组的大量信息.它还能够只显示与所指定的准则匹配的那些分组. netstat程序 netstat服务于多个目的: (1)展示网络端点 ...
- Orcla 数据库复习2 --子查询和表连接
子查询和表连接 ①.查询挣钱最多的人的名字 SELECT ename,sal FROM emp WHERE sal=(SELECT MAX(sal) FROM emp); ②.查询有哪些人的工 ...