There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.
We keep repeating the steps again, alternating left to right and right to left, until a single number remains.
Find the last number that remains starting with a list of length n.
Example:
解法1: Recursion
观察可以发现,对于[1,2,3,4,5,6,7,8,9] 这个数组,第一遍从左到右去掉之后得到的是[2,4,6,8],这个时候需要从右往左。
那么她的结果也就是可以看成是两倍的right(1,2,3,4)。
同理,这个(1,2,3,4)又可以看成是从左到右的(1,2),不过这里要区分一下奇数和偶数的情况。
解法2: Iteration TLE
也可以用约瑟夫环的类似的解法取去模拟删除的过程。不过大数据的时候就TLE了