首页 > 其他 > 详细

Lintcode214-Max of Array-Naive

时间:2019-04-01 12:58:12      阅读:145      评论:0      收藏:0      [点我收藏+]

Given an array with couple of float numbers. Return the max value of them.

Example

Example 1:

Input:  [1.0, 2.1, -3.3]
Output: 2.1	
Explanation: return the Max one.

Example 2:

Input:  [1.0, 1.0, -3.3]
Output: 1.0	
Explanation: return the Max one.


注意:

  1. maxNum要定义在for循环外面,否则return的时候找不到变量。定义在for循环内的变量,for循环外面无法访问。

  2. 表示Float的范围下限:  float maxNum = -Float.MAX_VALUE; 而Float.MIN_VALUE == 2-149 表示float精度的最小值, 是个正数。https://docs.oracle.com/javase/6/docs/api/java/lang/Float.html#MIN_VALUE

代码:

public float maxOfArray(float[] A) {
        float maxNum = -Float.MAX_VALUE;
        for (int i = 0; i < A.length; i++){
            maxNum = Math.max(maxNum, A[i]);
        }
        return maxNum;
    }

 

 

Lintcode214-Max of Array-Naive

原文:https://www.cnblogs.com/Jessiezyr/p/10635596.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!