Menu
CoddyTech

Find the Largest Number

你会得到一个非空整数列表 nums。返回其中最大的值。这些值可能是负数,所以答案也可能是负数。请用自己的比较来找出它,不要使用像 max 这样的内置最大值函数。

函数

findMax(nums: integer-array) → integer
numsinteger-array
要搜索的整数列表
返回值integer
nums 中的最大值

约束条件

  • 1 ≤ nums.length ≤ 5000
  • -109 ≤ nums[i] ≤ 109

示例

输入
nums = [3, 17, 4, 12, 9]
输出
17
解释
从左边开始读,目前最大的值是 3,然后是 17。4、12 和 9 都没有超过 17。
challenge icon

进阶

你能先成对比较这些值,用大约 3n/2 次比较找出最大值和最小值,而不是用 2n 次吗?

重置代码
def findMax(nums):
    # 在这里编写代码

测试 #1

测试 #2

测试 #3

参数

nums = [3, 17, 4, 12, 9]

预期输出

17