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 ...
随机推荐
- 升级struts到2.5.2遇到的问题及解决方案
原来的版本是2.3.x,由于安全原因需要升级到2.5.2.1,2.5.2版本不再提供xwork.jar ,整合到了 struts-core包中. 2,方法不能访问的问题,需要在每个action配置文件 ...
- 使用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 ...
- UDP套接字——(DGRAM)
/*********************程序相关信息********************* * 程序编号:014 * 程序编写起始日期:2013.11.29 * 程序编写完成日期:2013.1 ...
- JavaScript hash
location.hash -- 返回URL#后面的内容,如果没有#,返回空 hash,中文"哈希" 引用网址:http://www.dreamdu.com/javascript/ ...
- 一种Android数据请求框架
大部分Android应用一般都涉及到跟server的交互,除非是某些单机应用.既然要跟server打交道,向server请求数据差点儿是必做的事情,或许每家的APP都有一套自己的详细实现逻辑.但我们还 ...
- kCGImagePropertyExifDictionary 引用错误
kCGImagePropertyExifDictionary 引用错误 使用 AVFoundation拍照 //获取图片 [outputImage captureStillImageAsynchron ...
- Linux下全局符号覆盖问题
在windows上,默认情况下,动态库中的符号都是对外隐藏的,除非你显示的指出要导出哪些符号,否则外界是看不到的.但是linux下情况刚好相反,对静态变量和全局变量,linux下so里面的符号对外可见 ...
- 转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes)
转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl ...
- vue 表单 验证 async-validator
1.使用插件async-validator async-validator 地址:https://github.com/yiminghe/async-validator 2.示例(vue+elemen ...
- 查看sqlserver 2008中性能低下的语句
经常使用这个语句来查看性能低下的sql语句: SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total_physical ...