[C++]Rotate Array 旋转数组
2015-08-21 10:06
429 查看
leetcode 原题链接:https://leetcode.com/problems/rotate-array/
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array
rotated to
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
简要翻译:题目要求就是将一个数组右移k步,例如 长度为7 的数组[1, 2, 3, 4, 5, 6, 7] 右移3 步 就是讲最后三个数移动到前面来 即[5, 6, 7, 1, 2, 3, 4];
提示:尽力提出多种方法解决这个问题,至少有三种方法可以解决。
简要分析:
1、根据题目的意思,最容易想到的做法就是每次都将数组的最后一位移动到数组的第一位,这样每次的移动次数为n,一共要循环k次,因此时间复杂度为O(n*k)。
2、根据July大神的思路,我们可以采用三次移动的方式来完成,其中用到的公式是(X'Y')' = YX。即首先将0~size-k-1进行首尾交换操作,然后将size-k~size-1进行首尾交换操作,最后将整个数组进行首尾交换操作。时间复杂度为O(2n) = O(n)
实现代码
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
void rotate(vector<int>& nums, int k)
{
if (nums.empty() || k == 0)
return;
if (k >= nums.size())
k %= nums.size();
if (k < nums.size())
k = k%nums.size();
rotate(nums, 0, nums.size() - k-1);
rotate(nums, nums.size() - k, nums.size()-1);
rotate(nums, 0, nums.size() - 1);
return;
}
void rotate(vector<int>& nums, int start, int end)
{
int temp = 0;
while (start < end)
{
temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
return;
}
void display(vector<int> nums)
{
for (vector<int>::iterator p = nums.begin(); p != nums.end(); p++)
{
cout << *p << " ";
}
cout << endl;
}
};
int main()
{
int nums[] = { 1, 2, 3, 4, 5, 6, 7 };
vector<int> intVector(nums, nums + 7);
Solution st;
st.rotate(intVector, -2);
st.display(intVector);
return 0;
}
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.
简要翻译:题目要求就是将一个数组右移k步,例如 长度为7 的数组[1, 2, 3, 4, 5, 6, 7] 右移3 步 就是讲最后三个数移动到前面来 即[5, 6, 7, 1, 2, 3, 4];
提示:尽力提出多种方法解决这个问题,至少有三种方法可以解决。
简要分析:
1、根据题目的意思,最容易想到的做法就是每次都将数组的最后一位移动到数组的第一位,这样每次的移动次数为n,一共要循环k次,因此时间复杂度为O(n*k)。
2、根据July大神的思路,我们可以采用三次移动的方式来完成,其中用到的公式是(X'Y')' = YX。即首先将0~size-k-1进行首尾交换操作,然后将size-k~size-1进行首尾交换操作,最后将整个数组进行首尾交换操作。时间复杂度为O(2n) = O(n)
实现代码
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
void rotate(vector<int>& nums, int k)
{
if (nums.empty() || k == 0)
return;
if (k >= nums.size())
k %= nums.size();
if (k < nums.size())
k = k%nums.size();
rotate(nums, 0, nums.size() - k-1);
rotate(nums, nums.size() - k, nums.size()-1);
rotate(nums, 0, nums.size() - 1);
return;
}
void rotate(vector<int>& nums, int start, int end)
{
int temp = 0;
while (start < end)
{
temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
return;
}
void display(vector<int> nums)
{
for (vector<int>::iterator p = nums.begin(); p != nums.end(); p++)
{
cout << *p << " ";
}
cout << endl;
}
};
int main()
{
int nums[] = { 1, 2, 3, 4, 5, 6, 7 };
vector<int> intVector(nums, nums + 7);
Solution st;
st.rotate(intVector, -2);
st.display(intVector);
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- 动易2006序列号破解算法公布
- ruby 数组使用教程
- Ruby中的数组和散列表的使用详解
- C#实现AddRange为数组添加多个元素的方法
- C#动态调整数组大小的方法
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua中调用C++函数示例
- 详解Lua中的数组概念知识
- Lua教程(一):在C++中嵌入Lua脚本
- Perl中的列表和数组学习笔记
- Lua教程(二):C++和Lua相互传递数据示例
- 探索PowerShell (八) 数组、哈希表(附:复制粘贴技巧)
- C#中数组初始化与数组元素复制的方法
- C#交错数组用法实例
- 超大数据量存储常用数据库分表分库算法总结