Rotate Array
2015-11-25 19:07
435 查看
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
解决一:按步移(时间太长没有通过)
解决二:移址(时间短,效率高)
void rotate(int nums[], int n, int k) {
k = k % n;
int b
;
if (k == 0) return;
int *temp = b;
memcpy(temp, nums+(n-k), sizeof(int)*k);
memcpy(temp+k, nums, sizeof(int)*(n-k));
memcpy(nums, temp, sizeof(int)*n);
}
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array
[1,2,3,4,5,6,7]is rotated to
[5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
解决一:按步移(时间太长没有通过)
解决二:移址(时间短,效率高)
void rotate(int nums[], int n, int k) {
k = k % n;
int b
;
if (k == 0) return;
int *temp = b;
memcpy(temp, nums+(n-k), sizeof(int)*k);
memcpy(temp+k, nums, sizeof(int)*(n-k));
memcpy(nums, temp, sizeof(int)*n);
}
相关文章推荐
- 【转】华为Java编程军规,每季度代码验收标准
- IO流---文本文件读取方式一
- java SWT Browser 交互
- linux程序前后台切换
- 黑白图像
- 在Java中 获取 oracle数据库 空间字段 提取出 坐标值
- 雷军为啥那么煽情:回到初心,芝麻开门
- Contains Duplicate in c
- 路由消息填充
- Java基础<十三>------Map集合
- android 开发进阶自定义控件 类似 TextView
- java zip文件操作
- LINQ
- CentOS7编译安装Openvswitch 2.3.0 LTS
- Zabbix 监控Nginx_status 自动发现脚本
- 关于python中的setup.py
- 开启window7 wifi 热点
- JS的'=='、'==='、'!='、'!=='及数字字符串的'-'操作
- dtruss & strace
- C# FTP 上传 下载(汇总)