我想先说谢谢你的帮助。
我正在解决循环旋转问题,您必须将列表/数组的内容向右移动并有效地将元素包裹起来,例如:
例如,给定
A = [3, 8, 9, 7, 6]
K = 3
该函数应返回 [9, 7, 6, 3, 8]。进行了三个轮换:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
[6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
[7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
我的代码如下:
def solution(A, K):
new_list = []
first_index = 0
for i in range(0, K):
for num in range(0, len(A) - 1):
new_list.insert(0, A.pop())
print('new list {}'.format(new_list))
print('old list {}'.format(A))
if len(new_list) == 3:
new_list.insert(len(new_list), A.pop(first_index))
print(new_list)
经过 3 次旋转后,我得到的列表为 A = [8, 9, 7, 6, 3] 所以对我来说,它似乎将最后一个元素从 A 放置到 new_list 的前面。
因此,任何正确方向的帮助或点都会有所帮助,再次感谢您。