数组移动问题
来源:互联网 发布:录入数据 英文 编辑:程序博客网 时间:2024/05/17 02:45
#include<stdio.h>void shiftleft(int *pInOut, int n){int i;int tmp;tmp = *pInOut;for (i = 0; i < n - 1; ++i){*pInOut = *(pInOut + 1);pInOut++;}*pInOut = tmp;}void shiftright(int *pInOut, int n){int i;int tmp;pInOut += n - 1;tmp = *pInOut;for (i = n - 1 ; i > 0; --i){*pInOut = *(pInOut - 1);--pInOut;}*pInOut = tmp;}void shiftN(int *pInOut, int n, int shiftN){int i;if(shiftN > 0){for(i = 0; i < shiftN % n; ++i)shiftleft(pInOut, n);}else{shiftN = -shiftN;for(i = 0; i < shiftN % n; ++i)shiftright(pInOut, n);}}int main(){int array[4] = {1, 2, 3, 4};int array1[3] = {1, 2, 3};int i;shiftN(array, 4, 5);for(i = 0; i < 4; ++i)printf("%d ",array[i]);printf("\n"); shiftN(array1, 3, -1);for(i = 0; i < 3; ++i)printf("%d ",array1[i]);printf("\n");return 0;}
数组移动操作,写一个函数void shitfN(int *pInOut, int n, int shiftN),将pInOut指向的数组移动shiftN位,如果shiftN为正数,就左移,如果为负数就右移,n为数组的长度。例如,array[]={1, 2, 3, 4}, shiftN = 1,移动后的结果为,array[] ={ 2, 3, 4, 1}
如果array[]={1, 2, 3},shiftN = -1,移动后的结果为,array[]={3, 1, 2}
时间复杂度为o(ShiftN*n),换个方法思考:
循环左移ShiftN个,效果等价于,前ShiftN个元素逆置,后n-ShiftN个元素逆置,最后n个元素整体逆置。时间复杂度为o(n).
- 数组移动问题
- 数组和指针地址移动问题
- 数组移动 数组元素循环右移问题
- 数组移动
- 数组移动
- Using Cyclic Replacements解决数组原址移动问题
- C++ new一个数组时,指针移动程序崩溃问题
- 1008. 数组元素循环右移问题 (20)——数组循环移动算法
- 用数组移动字符串
- 数组移动算法
- 数组移动算法
- 数组数据循环移动
- 数组向右移动实现
- 循环移动数组元素
- 移动数组flag位!
- 移动数组中的元素
- 数组循环移动
- 移动数组中的0
- Ant打包编译部署工具学习笔记
- 合并多个PPT文件到一个文件中的方法
- 16.eclipse工具
- JQuery中区分模拟点击事件和用户点击事件
- 最小优先队列--堆实现
- 数组移动问题
- 内存泄漏
- 用javascript实现文本框中只能输入数字
- 17.HTML,CSS
- smarty快速学习教程
- 数据库范式速记
- 18.反射.枚举
- 内存管理读后摘记
- Eclipse构建Maven项目