给定一个整数数组:
- 从第一个数字开始
- 向前跳转n个位置,其中n是当前位置的值
- 删除当前位置,使下一个位置成为当前位置。
- 转到第2步,直到剩下一个数字为止
- 打印该号码
规则
数组环绕(数组中最后一个数字之后的下一个数字是第一个数字)。
零将自身删除(很明显)。
不允许输入负数。
测试用例
[1] => 1
[1,2] => 1
[1,2,3] => 3
[1,2,2] => 1
[1,2,3,4] => 1
[6,2,3,4] => 4
[1,2,3,4,5] => 5
[0,1] => 1
[0,0,2,0,0] => 0
分步示例
[1,4,2,3,5]
^ start from the first position
^ jump 1 position (value of the position)
[1, 2,3,5] remove number in that position
^ take next position of the removed number (the 'new' 'current' position)
^ jump 2 positions
[1, 2,3 ] remove number in that position
^ take next position (looping on the end of the array)
^ jump 1 position
[1, 3 ] remove number in that position
^ take next position (looping)
^ jump 3 positions (looping on the end of the array)
[ 3 ] remove number in that position
print 3
范例#2
[4,3,2,1,6,3]
^ start from the first position
^ jump 4 positions
[4,3,2,1, 3] remove number in that position
^ take next position
^ jump 3 positions
[4,3, 1, 3] remove number in that position
^ take next position
^ jump 1 positions
[4,3, 1 ] remove number in that position
^ take next position
^ jump 4 positions
[4, 1 ] remove number in that position
^ take next position
^ jump 1 position
[ 1 ] remove number in that position
print 1
这是代码高尔夫球,最短的答案以字节为单位!