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

如数组 [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. Flask项目示例目录

    Flask不同于Django,Django在创建程序时自动得到必要的目录文件,而Flask则只有一个空文件夹,所以关于Flask项目的目录我们需要自行配置. 首先利用pycharm创建一个项目,在根目 ...

  2. 【转】AI类人工智能产品经理的丛林法则

    本文转载自:https://blog.csdn.net/buptgshengod/article/details/77030338 AI是大家都很关注的领域,然而对于大部分想要入行的同学来讲,AI的算 ...

  3. 20165215 2017-2018-2《Java程序设计》课程总结

    20165215 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 预备作业1:我期望的师生关系:令我记忆深刻的老师,期望的师生关系,本学期的学习规划. 预备作业二 ...

  4. 创建servlet程序知识点详解---servlet-day12

    自定义标签 (1)编程步骤 step1 jsp标签分为复杂标签技术(old),简单标签(new) 注(了解) jsp标签技术分为复杂标签技术(old),简单标签技术(new) step2 ###MVC ...

  5. 外网访问内网Elasticsearch WEB

    外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...

  6. SCI_Call_Bsw_SetPwmMotorGroupB

    Sci_Bsw.c -- definition MotorGroupB_cfg.c -- called in LatchControl_Magna.c: extern uint16 Sci_DRead ...

  7. Shell data、timedatectl

     data系统时间管理命令 命令date +%F xxxx—xx--xx #查看当前日期. 命令date +%T xx:xx:xx #查看当前时间. 命令date +%y xx #年2位 命令date ...

  8. 修改mongodb(带仲裁节点的副本集)各机器端口

    需求:因为端口调整,需要改变副本的备份集 1.查看当前的副本集信息 [root@localhost bin]# ./mongo 192.168.1.134:10001 repltest:PRIMARY ...

  9. [Python]基础教程(2)、PyCharm安装及中文编码

    一.PyCharm安装 http://blog.csdn.net/yctjin/article/details/70307933?locationNum=11&fps=1 这篇文章写得及其详细 ...

  10. gitlab 迁移

    http://www.cnblogs.com/crysmile/p/9505527.html