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 适合初学者 ----------------------------------------------------------- ...
随机推荐
- 稀疏数组(java实现)
1.稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.1记录数组一共有几行几列,有多少个不同的值 1.2把具有不同值的元素的行列 ...
- mysql数据库,数据表,数据的增删查改语句
查询mysql支持的引擎 show engines; 查询mysql支持的字符集 show character set; 设置mysql默认存储引擎 set default_storage_engin ...
- 小贴士--java篇
1. java: “.”和“|”都是转义字符,必须得加"\\" 2.java :如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如:“acount=? and uu = ...
- lock的等效代码
1.lock的等效代码 在.NET的多线程程序中,经常会遇到lock关键字来控制同步,比如下列代码: private object o = new object(); public void Work ...
- C#数字日期转成中文日期
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- JS 页面刷新以及页面返回的几种方式
1.通过标签形式的跳转页面 <a class="popup" href="~/WeiXin/Shoppingguide/StockData">&l ...
- 1 spring如何通过组件扫描和自动装配实现自动化的配置
1 首先将spring依赖的包全部导入 2 建立测试接口 public interface CompactDisc { void play(); } 3 具体的类实现接口 import org.spr ...
- FICO-错误日志集
1.F-02报错 System error in routine FI_TAX_CHK_PRICING_DATA error code 13 function builder TAX2 程序 FI_T ...
- 安卓开发之cache 的使用(图片查看器案例)
package com.lidaochen.test; import android.graphics.Bitmap; import android.graphics.BitmapFactory; i ...
- ios数组倒序
比如有一个数组: NSArray *arr = @["]; 倒过来排序: arr = [[arr reverseObjectEnumerator] allObjects]; NSMutabl ...