数组无限长 [英] Array of undefinite length

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

问题描述

如何在未知长度的排序数组中找到随机元素。

how to find a random element in a sorted array of unknown length.

推荐答案

我会假设你的意思是如何查找元素是否是数组的一部分? not 如何从数组返回一个随机元素?

I'll assume you mean how do I find if an element is part of the array? not how do I return a random element from the array?.

使用二进制搜索并假设那长度很大(肯定你有上限?)。如果中间元素 m ,则您在每个步骤中选择都不在数组边界之外(您需要一种方式来告诉这一点),然后将搜索限制在索引小于 m

Use binary search and assume that the length is very big (surely you have an upper bound?). If the middle element m you select at each step is outside the array bounds (you need a way to tell this), then limit the search to those elements with indexes small than m.

如果没有办法告诉元素是否在数组的边界之外我看不到你如何解决这个问题。

If you don't have a way to tell if an element is outside the bounds of the array then I don't see how you could solve this.

这篇关于数组无限长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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