实现数组旋转(循环右移)

如数组 [1, 2, 3, 4, 5, 6, 7],右移 3 位则为 [5, 6, 7, 1, 2, 3, 4]

首先使用泛型函数

void Rotate(void *front, void *middle, void *last) {
int frontSize = (char *)middle - (char *)front;
int backSize = (char *)last - (char *)middle;
char *buffer = (char *)malloc(frontSize);
memcpy(buffer, front, frontSize);
memmove(front, middle, backSize);
memcpy((char *)last - frontSize, buffer, frontSize);
free(buffer);
}

分析:

1、由于 front、middle、last 都是 void 型指针,不能进行指针加减法运算,这里依旧使用转换成 char * 的技巧,得到两个地址之间的实际的物理字节总数。

2、待拷贝的源区域和目的区域是可重叠的,因此使用 memmove() 代替 memcpy(),memmove 在拷贝的内存区域有重叠时,能够智能地选择正序(从低地址开始拷贝)或倒序(从高地址开始拷贝)。但我在查阅网上资料和自己测试发现,目前 memcpy 也能识别这种情况。

LeetCode 189 以及 PAT 乙级 1008 里都是这题,题目要求不使用额外的数组空间,只允许通过 swap() 交换单个元素实现。

【C/C++】Rotate Array的更多相关文章

  1. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  2. 【LeetCode题解】数组Array

    1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...

  3. 【37.38%】【codeforces 722C】Destroying Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【LeetCode】【矩阵旋转】Rotate Image

    描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  5. 【Codeforces 722C】Destroying Array (数据结构、set)

    题意 输入一个含有 n(1≤n≤100000) 个非负整数的 a 数组和一个 1-n 的排列 p 数组,求每次删除 a[p[i]] 后,最大连续子段和(不能跨越被删除的)是多少? 分析 因为都是非负整 ...

  6. 【Gym 100947C】Rotate It !!

    分两类,奇数和偶数的,用隔项前缀和算一下. #include <algorithm> #include <iostream> #define N 10005 using nam ...

  7. phpRedis函数使用总结【分类详细】

    <?php /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host, ...

  8. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  9. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

    前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...

随机推荐

  1. [js]变量提升-关于条件

    条件函数变量提示于全局中函数变量提升不一样. 条件中: 函数变量提升, 只是声明(现新版本浏览器中) if(g()){ function g() { return true } console.log ...

  2. linux 进程间通信——内存共享映射mmap和munmap

    IPC三种通信机制是指:信号量.共享内存.消息队列,   信号量:通过操作系统中的PV操作来实现: 共享内存:申请一块内存,进程A往共享内存中写,其他的进程就可以通过读出共享内存中的内容来获取进程A所 ...

  3. arch----------arch下的一些命令,亲测

    1.taoyanghao 不在 sudoers 文件中.此事将被报告. 这个是使用sudo以后报出的错误提示,sudo确定已经安装了. 解决方案:编辑/etc/sudoers文件.找到这一 行:&qu ...

  4. jquery对复选框(checkbox)的操作(精华)

    @{ Layout = null;} <!DOCTYPE html> <html><head> <meta name="viewport" ...

  5. 笔记-ASP.NET WebApi

    本文是针对ASP.NET WepApi 2 的笔记. Web API 可返回的结果: 1.void 2.HttpResponseMessage 3.IHttpActionResult 4.其他类型 返 ...

  6. matlab之中文字体乱码处理

  7. GDScript 格式化字符串

    GDScript offers a feature called format strings, which allows reusing text templates to succinctly c ...

  8. Feign 与 Hystrix

    Feign 与 Hystrix Feign是一个声明式的web服务客户端,它使得web服务调用非常的简单,当我们使用Feign时,Spring Cloud 整合了Ribbon和Eureka,从而为我们 ...

  9. 0004-20180422-自动化第五章-python基础学习笔记

    内容回顾:1.数据类型 2.for和while循环 continue break #如下循环将怎么打印结果? for i in range(1,10): print(i) for i in range ...

  10. windows 允许其他电脑访问本地mysql数据库

    第一步:用ping命令测试两台电脑是否连通 如果两台电脑是连通的请转到第二步,如果是非连通的请进行如下操作: 1.进入控制面板,打开Windows Defender 防火墙,点击高级设置(本人用的是W ...