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 ...
随机推荐
- python的单元测试框架
1.unittest是Python内置的标准类库.它的API跟Java的JUnit..net的NUnit,C++的CppUnit很相似. 通过继承unittest.TestCase来创建一个测试用 ...
- vs 字体
看代码看得眼疼不能不说是程序员的恶梦,那么,选择适当的字体也算是对自己的救赎吧.周末闲得无聊,在网上乱逛,搜索了一些资料整理一下给大家分享,仅作记录而已,参考使用: 1.一个编程人员痛苦的选择 一般适 ...
- 【Python】Django CSRF问题
参考资料: Django Ajax CSRF 认证:http://www.ziqiangxuetang.com/django/django-csrf.html Python Post遇到csrftok ...
- [译]使用scikit-learn进行机器学习的简介(教程1)
原文:http://www.cnblogs.com/taceywong/p/4568806.html 原文地址:http://scikit-learn.org/stable/tutorial/basi ...
- 无法将“<s:SimpleText>”解析为组件执行
导入以前版本的flex代码时,报"无法将“<s:SimpleText>”解析为组件执行"错误: 后来在网上找到解决方案,即版本更新的一段话: 网址是:https://f ...
- function/bind
1.函数指针指向一类函数,这类函数的类型一样,也就是函数的返回类型和形参表一样. 2.不同的函数类型要使用不同的函数指针,才能指向它,有没有好的办法呢? 类比思考下,交换方法,对不同的类型要写不同的s ...
- Wifidog及认证过程初分析
Wifidog初分析 一.综述 wifidog是搭建无线热点认证系统的解决方案之一,他比nocat.nodog更适合互联网营销思路.常见的使用在openwrt系统上,它实现了路由器和认证服务器的数据交 ...
- 【JavaScript】2013年人气最高的JavaScript框架排名
本文概述 本文介绍2013年人气急速上升,2014年必须知道的JavaScript框架排名.本文所介绍的排名为Google根据全世界2013年的搜索关键词所做出的统计结果. MVC框架 JavaScr ...
- 算法笔记_028:字符串转换成整数(Java)
1 问题描述 输入一个由数字组成的字符串,请把它转换成整数并输出.例如,输入字符串“123”,输出整数123. 请写出一个函数实现该功能,不能使用库函数. 2 解决方案 解答本问题的基本思路:从左至右 ...
- Python list删除元素
pop()方法 pop(n) 从list删除元素Paul同学刚来几天又要转走了,那么我们怎么把Paul 从现有的list中删除呢?如果Paul同学排在最后一个,我们可以用list的pop()方法删除: ...