【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.操作方法 ...
随机推荐
- 【托业】新托业全真题库---TEST1
clearly indicate ——clearly可以修饰indicate(表明:暗示:指示) recently只用于现在完成时和过去完成时中 municipal gallery 市立美术馆 per ...
- Spring Security 安全验证
摘自:https://www.cnblogs.com/shiyu404/p/6530894.html 这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解 ...
- 一个项目中mysql数据库经常死锁的问题解决记录
1.问题描述 此项目为一个物流系统,需要使用PDA对货物进行入库.备货.出货等操作,在系统开发测试过程中,经常发现死锁问题. 有这样一种业务场景:仓库对备货单上货进行扫码备货后,点击"完成& ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)
可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...
- Unicode编码与中文互转
/** * unicode编码转换为汉字 * @param unicodeStr 待转化的编码 * @return 返回转化后的汉子 */ public static String UnicodeTo ...
- caffe-ssd编译runtest时候报错:g++: internal compiler error: Killed (program cc1plus)
大哥,你的内存不够了,删点儿东西吧
- day03 Python字典dict的增删查改及常用操作
字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可 ...
- [openjudge-搜索]湖的深度
题目描述 描述 一个湖用 R x C (1 ≤ R ≤ 50; 1 ≤ C ≤ 50) 的网格表示.格点上的非负整数 D_rc (0 ≤ D_rc ≤ 1,000,000)表示该位置的深度.整数0表示 ...
- java中的ElasticSearch搜索引擎介绍。
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...
- bind 笔记
BIND 的安装和配置: dns 服务,程序包名为bind ,程序名称为named 只需要安装 bind ,bind-libs ,bind-utils 生产环境全部安装 bind 服务脚本 :/etc ...