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大。

最新文章

  1. php使用post方式获得文件扩展名
  2. 1.Java基础之System对象
  3. C# Enum Name String Description之间的相互转换
  4. Logging configuration
  5. PHP开发环境配置
  6. jsp页面不能使用EL表达式
  7. matlab 怎么保存plot的图 到指定文件夹
  8. leetcode-WordLadder
  9. [Swift]LeetCode423. 从英文中重建数字 | Reconstruct Original Digits from English
  10. c++ cout、cin、endl
  11. scrapy框架原理学习
  12. 依赖注入容器之Castle Windsor
  13. python 穷举法 算24点(史上最简短代码)
  14. Latex中如何设置字体颜色(3种方式)
  15. python 中dir()和__dict__的区别
  16. 获取Spring项目配置文件元素
  17. 【转】iOS - SQLite 数据库存储
  18. LOJ P3960 列队 树状数组 vector
  19. 记录一次teamview无法远程连接对方teamview的过程
  20. 此时本机的BootLoader程序坏了,也就是说grub第一阶段坏掉了,该如何修复

热门文章

  1. 在pycharm中进行nosetests并输出测试报告
  2. 数学图形(1.39)TN constant curve
  3. Linux下Oracle 10g DataGuard配置(主从同步及切换)
  4. 如何快速把安卓应用移植到BlackBerry 10上
  5. Eclipse 构建Maven项目--普通web项目 复制另外一个项目的配置文件导致的问题
  6. /etc/ssh/sshd_config 配置(ssh连接)
  7. GTID数据库备份
  8. 如何判断linux用户是否为root用户
  9. 移动端HTML5框架
  10. vue - 制作模板