题解 | #旋转数组#

旋转数组

https://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042?tpId=295&tqId=1024689&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj

C语言

int* solve(int n, int m, int* a, int aLen, int* returnSize ) {
    // write code here
    m = m%n;
    *returnSize = aLen;
    int last,temp;
    for(int i=0; i<m; i++){
        {
            last=a[aLen-1];             //先保存最后一个元素
            for(int j=aLen-1; j>0; j--){    //从倒数第二个开始,依次把元素赋给下一个,相等于除了最后一个元素,整体右移
                a[j] = a[j-1];
            }
            a[0] = last;                //将原数组最后一个元素赋值给首位
        }
    }
    return a;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务