邓英超 发表于 2013-7-30 15:41:18

[google面试题]given 
a
 deck
 of
 cards,
how
 can
 you
 shuffle
 them
 such 
that
 any
 permutation
 of
 cards
 is
 equally
 likely?

发表于 2013-7-30 16:04:26

Good answer:Go through the elements in order,swapping each element with arandom element in the array that doesnot appear earlier than the element.Thistakes O(n) time.
页: [1]
查看完整版本: [google面试题]given 
a
 deck
 of
 cards,
how
 can
 you
 shuffle
 them
 such 
that
 any
 permutation
 of
 cards
 is
 equally
 likely?