Second Max of Array
Find the second max number in a given array.
Example
Given [1, 3, 2, 4], return 3.
Given [1, 2], return 1.
Notice : Attention corner case. Communicate with Interviewer.
public class Solution {
/**
* @param nums: An integer array.
* @return: The second max number in the array.
*/
public int secondMax(int[] nums) {
int max = Math.max(nums[0],nums[1]);
int secondMax = Math.min(nums[0],nums[1]);
for (int i = 2; i < nums.length; i++) {
if (max <= nums[i]) {
secondMax = max;
max = Math.max(nums[i],max);
}
}
return secondMax;
}
}
Second Max of Array的更多相关文章
- 2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]
[本文链接] http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html [题目] 对于一个由N个整数组成的数组,需要比较多少次才能把 ...
- 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 n ...
- Scala学习笔记之:tuple、array、Map
[TOC] 本文<快学Scala>的笔记 tuple学习笔记 tuple的定义 对偶是元组(tuple)的最简单形态--元组是不同类型的值的聚集. 元组的值是通过将单个值包含在圆括号中构成 ...
- 找出numpy array数组的最值及其索引
在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引 但在numpy中的array没有index方法,取而代之的是where ...
- 整理:Javascript获取数组中的最大值和最小值的方法汇总
方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //最小值 Array.prototype.min = function ...
- 读书笔记-JavaScript面向对象编程(一)
PDF下载链接: http://pan.baidu.com/s/1eSDSTVW 密码: 75jr 第1章 引言 1.1 回顾历史 1.2 变革之风 1.3 分析现状 1.4 展望未来 1.5 面向对 ...
- 通用js函数集锦<来源于网络> 【二】
通用js函数集锦<来源于网络> [二] 1.数组方法集2.cookie方法集3.url方法集4.正则表达式方法集5.字符串方法集6.加密方法集7.日期方法集8.浏览器检测方法集9.json ...
- javascript的缓动效果
这部分对原先的缓动函数进行抽象化,并结合缓动公式进行强化.成品的效果非常惊人逆天.走过路过不要错过. 好了,打诨到此为止.普通的加速减速是难以让人满意的,为了实现弹簧等让人眼花缭乱的效果必须动用缓动公 ...
- C语言经典例题100
C语言经典例题100 来源 http://www.fishc.com 适合初学者 ----------------------------------------------------------- ...
随机推荐
- 036 Android Xutils3网络请求框架使用
1.xUtils3 介绍 xUtils 中目前包括了主要的四大模块,分别为 DbUtils 模块.ViewUtils 模块.HttpUtils 模块以及 BitmapUtils 模块. xUtils3 ...
- 第五章 模块之 shtil 、 json / pickle、importlib、collections
5.8 shtil 高级的 文件.文件夹.压缩包 处理模块 shutil.rmtree 删除目录 import shutilshutil.rmtree(path) shutil.move 重命名:移动 ...
- C++打印杨辉三角形
#include <iostream> #include <iomanip> #include <Windows.h> using namespace std; # ...
- oracle笔记之计算年龄、工龄和TRUNC
方法一:利用months_between 函数计算 SELECT TRUNC(months_between(sysdate, birthday)/12) AS agefrom dual; 方法二:日期 ...
- C# 字符串按设置的格试在前面或后面增加空格或其它字符
public string lengadd(string stringa, string stringb, int count, int mode) //以stringa的长度,未到count的长度则 ...
- 写在NOIP2018前
不知不觉距离NOIP2018还有两天,两个月的停课生活即将结束 此时心里总感觉装着许多话,想要将其倾诉却发现连哪怕一句也凝结不出 只觉得这两月像是场荒诞的冒险,好像我想做的什么都做了,又感觉我其实一事 ...
- Django 报错总结
报错: AttributeError: 'NoneType' object has no attribute 'split' 最近在写网站中遇到一个问题,就是题目上所写的:AttributeError ...
- Django_rest_framework分页
分页基本流程及配置 1.基于LimitOffsetPagination做分页,根据配置 from rest_framework.pagination import LimitOffsetPaginat ...
- ElementUI对话框(dialog)提取为子组件
需求:在页面的代码太多,想把弹窗代码提取为子组件,复用也方便. 这里涉及到弹窗el-dialog的一个属性show-close: show-close="false"是设置不显 ...
- Scrapy 安装与使用
Scrapy的安装: 当前环境win10,python_3.6.4,64bit.在命令提示符窗口运行pip install Scrapy,出现以下结果: building 'twisted.test. ...