undefinite长数组 [英] Array of undefinite length
问题描述
如何找到一个随机元素未知长度的有序数组。
how to find a random element in a sorted array of unknown length.
推荐答案
我会认为你的意思是怎么做,我觉得如果一个元素是数组的一部分?
不如何返回随机元素的数组?
。
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.
这篇关于undefinite长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!