Python doc说切片列表会返回一个新列表.现在,如果返回"新"列表,我有以下与"分配给切片"有关的疑问
a = [1, 2, 3]
a[0:2] = [4, 5]
print a
Run Code Online (Sandbox Code Playgroud)
现在输出将是:
[4, 5, 3]
Run Code Online (Sandbox Code Playgroud)
我最近了解了当我在Programming Pearls书中阅读解决方案时,Juggling算法如何在线性时间内旋转数组.
解决它的代码如下:
/*Function to left rotate arr[] of siz n by d*/
void leftRotate(int arr[], int d, int n)
{
int i, j, k, temp;
for (i = 0; i < gcd(d, n); i++)
{
/* move i-th values of blocks */
temp = arr[i];
j = i;
while(1)
{
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] …Run Code Online (Sandbox Code Playgroud)