忘了,但是我自己reword了一下写到笔记里了 Given Rand(int n), return a random number from 0 ~ n Now design a Rand(int n, int[] except) which output number should not in except array
【在 e***s 的大作中提到】 : 忘了,但是我自己reword了一下写到笔记里了 : Given Rand(int n), return a random number from 0 ~ n : Now design a Rand(int n, int[] except) which output number should not in : except array
M*n
6 楼
对,就是这个,问题是except太大了怎么办?比如n是1000,excep里面有980个数。
【在 e***s 的大作中提到】 : 忘了,但是我自己reword了一下写到笔记里了 : Given Rand(int n), return a random number from 0 ~ n : Now design a Rand(int n, int[] except) which output number should not in : except array
e*s
7 楼
我不是很懂你的方法,binary search用在哪里? 我的方法是, Sort except array O(nlogn) create new 0 ~ (n-k) array O(n) Get random number in new array O(1) 整个算法 O(nlogn)
how can you get random number from the new created array with size n-k given the n size random generator? I don't think n mod n-k will give you uniform distribution.
我不是很懂你的方法,binary search用在哪里?我的方法是,Sort except array O( nlogn)create new 0 ~ (n-k) array O........ ★ Sent from iPhone App: iReader Mitbbs Lite 7.56
【在 e***s 的大作中提到】 : 我不是很懂你的方法,binary search用在哪里? : 我的方法是, : Sort except array O(nlogn) : create new 0 ~ (n-k) array O(n) : Get random number in new array O(1) : 整个算法 O(nlogn)
e*s
9 楼
newArray[Rand(n-k)]
given
【在 h****n 的大作中提到】 : how can you get random number from the new created array with size n-k given : the n size random generator? : I don't think n mod n-k will give you uniform distribution. : : 我不是很懂你的方法,binary search用在哪里?我的方法是,Sort except array O( : nlogn)create new 0 ~ (n-k) array O........ : ★ Sent from iPhone App: iReader Mitbbs Lite 7.56
h*n
10 楼
you are not given the rand(n-k) that is my question . Rand(n)mod n-k will not work since the resulted distribution is not uniform
newArray[Rand(n-k)] ★ Sent from iPhone App: iReader Mitbbs Lite 7.56
【在 e***s 的大作中提到】 : newArray[Rand(n-k)] : : given
e*s
11 楼
他给Rand(n),n 不是应该随你输入什么吗?不是规定了一定要input的那个n啊!
【在 h****n 的大作中提到】 : you are not given the rand(n-k) that is my question . Rand(n)mod n-k will : not work since the resulted distribution is not uniform : : newArray[Rand(n-k)] : ★ Sent from iPhone App: iReader Mitbbs Lite 7.56