[C语言]Leetcode 189 Rotate Array
来源:互联网 发布:淘宝网首页官网登录 编辑:程序博客网 时间:2024/06/10 05:56
Rotate Array
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].
void rotate(int* nums, int numsSize, int k) { if (k >=numsSize) k = k%numsSize; if (k == 0) return; int *arr; int i, j; arr = (int *)malloc(k*sizeof(int)); for (i = 0; i<k; i++) { *(arr + i) = *(nums + numsSize - k + i); } for (j = 0; j < numsSize - k; j++) { *(nums + numsSize - 1 - j) = *(nums + numsSize - k - 1 - j); } for (i = 0; i<k; i++) { *(nums + i) = *(arr + i); } return;}
加粗斜体部分需要注意,如果直接写成(*int)malloc(int),则会报错runtime error,
“`
1 0
- [leetcode]C语言:189 Rotate Array
- [C语言][LeetCode][189]Rotate Array
- [C语言]Leetcode 189 Rotate Array
- LeetCode::Rotate Array C语言
- [leetcode]Rotate Array C语言
- [leetcode-189]Rotate Array(c)
- 【LeetCode-189】Rotate Array(C++)
- 6---LeetCode【tag: Array】【Rotate Array】|C语言|总结
- [LeetCode][189][Rotate Array]
- Leetcode 189: Rotate Array
- leetCode #189 Rotate Array
- LeetCode #189 Rotate Array
- LeetCode(189) Rotate Array
- [LeetCode 189]Rotate Array
- LeetCode #189 Rotate Array
- LeetCode 189 Rotate Array
- leetcode-189 Rotate Array
- leetcode 189 Rotate Array
- 面向对象_猫狗案例加入跳高功能代码实现
- 勾股定理一日一证连载133
- 访问长度为0的vector引起的低概率coredump问题的定位
- 单链表的基本操作与算法(Java实现)
- win7 如何硬盘分区?
- [C语言]Leetcode 189 Rotate Array
- java 常用快捷键汇总
- 【事故】项目开发中的生产事故
- android webview js交互, 响应webview中的图片点击事件 ,点击(双击)使图片变大,单击退出
- JSP脚本中的九个内置对象
- A new world for me
- 【算法设计与分析基础】动态规划法 求两个序列的所有最长公共子序列及其长度
- win10系统升级后触摸板点击没反应
- IAR J-Link下载程序出现错误提示:Failed to get CPU status after 4 retries Retry?