有效的方式来找到一个数组中的最大数 [英] Efficient way to find the max number in an array

查看:153
本文介绍了有效的方式来找到一个数组中的最大数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个面试问题 有一个整数数组。数组中的元素可以按照下面的方式。

This is an interview question There is an array of integers. The elements in the array can follow the following patterns.

  1. 数字是按升序排列
  2. 数字是按降序排列
  3. 数字增加在开始和结束
  4. 减小
  5. 号码到底
  6. 开头和增加减少
  1. numbers are in ascending order
  2. numbers are in descending order
  3. numbers increases in the beginning and decreases in the end
  4. numbers decreases in the beginning and increases in the end

什么是有效的方式来发现阵列中的最大数量?

What is the efficient way to find the max number in the array?

推荐答案

在这种情况下,所有你需要做的就是确定它是否是(3)。如果不是,则答案为max(第一,最后一个)。

In that case, all you need to do is to determine whether it's (3). If not, the answer is max(first, last).

在所有的元素都相等的情况下,你需要大量搜索阵列表明,有没有一人多高数量在中间某个地方。所以,我认为这是为O(n),以确定是否是在(3)。

In the case that all elements are equal, you'll need to exhaustively search the array to show that there's not one high number somewhere in the middle. So I think it's O(n) to determine whether you're in (3).

这篇关于有效的方式来找到一个数组中的最大数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆