Lintcode214-Max of Array-Naive
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.
注意:
-
maxNum要定义在for循环外面,否则return的时候找不到变量。定义在for循环内的变量,for循环外面无法访问。
-
表示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; }