[Leetcode] 189.Rotate Array
来源:互联网 发布:程序员转行做销售 编辑:程序博客网 时间:2024/04/30 04:25
class Solution {public: void rotate(int nums[], int n, int k) { k %= n; reverse(nums, n-k, n-1); reverse(nums, 0, n-k-1); reverse(nums, 0, n-1); } void reverse(int s[], int start, int end) { while (start < end) { int t = s[start]; s[start++] = s[end]; s[end--] = t; } }};
tip:
1. 这个题目是右移k位,可以转换成左移n-k位,这样就回到之前的字符串左移的问题 (X’Y’)’ = YX;
2. 注意:reverse里交换两个位置的值时,需要开辟一个临时空间来存储;
时间复杂度O(n),空间复杂度O(1)
0 0
- [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
- 189. Rotate Array LeetCode
- [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
- C语言printf()函数的%*s输出
- 关于 (&a+1) 的困惑
- 层次化查询(START BY ... CONNECT BY PRIOR)
- 寒假杭电acm 4
- LOB 代表大对象数据,包括 BLOB 和 CLOB 两种类型
- [Leetcode] 189.Rotate Array
- 蘑菇智能自行车系统 改变你的自行车
- 2015年4月第六届蓝桥杯c/c++B组省赛试题和个人解答
- oracle 用myibatis 批量操作
- C语言实现迷宫求解
- 详解 Android 的 Activity 组件 转载
- BZOJ1529【DFS】
- POJ 1715- Hexadecimal Numbers(组合数学)
- 前端那些事儿(3) --- WEB架构的历史变迁