【C/C++】Rotate Array
实现数组旋转(循环右移)
如数组 [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的更多相关文章
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【LeetCode题解】数组Array
1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...
- 【37.38%】【codeforces 722C】Destroying Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【LeetCode】【矩阵旋转】Rotate Image
描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- 【Codeforces 722C】Destroying Array (数据结构、set)
题意 输入一个含有 n(1≤n≤100000) 个非负整数的 a 数组和一个 1-n 的排列 p 数组,求每次删除 a[p[i]] 后,最大连续子段和(不能跨越被删除的)是多少? 分析 因为都是非负整 ...
- 【Gym 100947C】Rotate It !!
分两类,奇数和偶数的,用隔项前缀和算一下. #include <algorithm> #include <iostream> #define N 10005 using nam ...
- phpRedis函数使用总结【分类详细】
<?php /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host, ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型
前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...
随机推荐
- Rosserial实现Windows-ROS交互操作
安装 sudo apt-get install ros-indigo-rosserial-windows sudo apt-get install ros-indigo-rosserial-serve ...
- js 整数型数组和字符型数组相互转换
需求背景: 需要将 a = [1,2,3,4,5] 转换成 a = ['1','2','3','4','5'](整数型数组转换成字符型没找到直接的方法,思路就是先将数组转换成字符串,然后再将字符串转 ...
- MySql 创建索引原则
https://blog.csdn.net/csdnones/article/details/50412603 为了使索引的使用效率更高,在创建索引时,必须考虑在哪些字段上创建索引和创建什么类型的索引 ...
- Python数据分析与挖掘常用模块
python在数据科学方面需要用到的库: a.Numpy:科学计算库.提供矩阵运算的库. b.Pandas:数据分析处理库 c.scipy:数值计算库.提供数值积分和常微分方程组求解算法.提供了一个非 ...
- Ch01 基础 - 练习
1. 在Scala REPL 中键入3.,然后按Tab键.有哪些方法可以被应用? scala> 3. % * - > >> ^ ...
- fastjson java类、字符串、jsonObject之前的转换
json对象转成json字符串 JSONObject json = new JSONObject(); json.put("page",1); json.put("pag ...
- c# 传入c++dll 回调函数输出byte 导致 bug
- windows----------windows查看端口是否被占用
假如我们需要确定谁占用了我们的80端口在windows命令行窗口下执行: netstat -aon|findstr "80" TCP 127.0.0.1:80 0.0.0.0:0 ...
- WxWidgets笔记
关于环境变量的配置:解压wxwidgets的压缩包之后要新建名为 WXWIN 的环境变量,变量的值为 解压得到的目录,不知为何要使用此环境变量 编译时使用的命令:mingw32-make -j1 -f ...
- tf.InteractiveSession()和tf.Session()
tf.InteractiveSession()适合用于python交互环境 tf.Session()适合用于源代码中 1.tf.InteractiveSession() 直接用eval()就可以直接获 ...