把as_mut_slices
和rotate_right
加起来.
fn sort_vecdeque<T: Ord>(x: &mut VecDeque<T>) {
x.rotate_right(x.as_slices().1.len());
assert!(x.as_slices().1.is_empty());
x.as_mut_slices().0.sort();
}
这是因为VecDeque
等于implemented:这是一个单一的内存分配,在结尾和开始(如果是head > tail
)或中间(如果是head < tail
)都有空洞.
- 如果孔位于起点和终点,则元素已经是连续的.
- If the hole is in the middle, the front of the deque is at the end of
the allocation, and the back of the deque is at the beginning of the allocation.
rotate_right
"pops the
last k items and pushes them to the front.", i.e. it removes the
elements at the start of the allocation and adds them to the elements
at the back of the allocation, so the deque will be contiguous.
(The way I read the docs, there's not really a guarantee that it will always be that way, which is why I like having the assert there.)
最后,如果二人组是连续的,
as_slice
(和它的mut
个朋友)
将返回两个切片,但第二个切片将为空,所有元素都在第一个切片中.
此外,使用SO进行编码比赛可能会被认为是作弊.