leetcode: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]
.
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Related problem: Reverse Words in a String II
分析:题意为 将n个元素的数组向右旋转k步
思路:用vector容器的东西来做很简单
代码如下:(O(1) Space)
class Solution {
public:
void rotate(vector<int>& nums, int k)
{
for(int i=0;i<k;i++)
{
nums.insert(nums.begin(), nums.back());
nums.pop_back();
}
}
};
然后换种方法:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
vector<int> v(n);
for(int i=n-k,j=0;i<n-1,j<k-1;i++,j++){
v[j]=nums[i];
}
for(int i=0,j=k;i<n-k-1,j<n-1;i++,j++){
v[j]=nums[i];
}
nums=v;
}
};
出错:Last executed input:[1,2,3,4,5,6], 11
因为没有考虑到当k大于n的情况,所以需要改进:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
vector<int> rot(n);
for(int i = 0; i < n; i++) {
if((i + k) < n) rot[i + k] = nums[i];
if((i + k) >= n) {
rot[(i + k)%n] = nums[i];
}
}
nums = rot;
}
};
c语言
看看:
void rotate(int* nums, int numsSize, int k) {
int i;
if(k > numsSize)
k -= numsSize;
int* temp = (int*)calloc(sizeof(int), numsSize);
for(i = 0; i < k; i++)
temp[i] = nums[numsSize - k + i];
for(; i < numsSize; i++)
temp[i] = nums[i - k]; for(i = 0; i < numsSize; i++)
nums[i] = temp[i];
}
或:
void reverse(int *nums, int start, int end) {
int tmp;
while (start < end) {
tmp=nums[start];
nums[start]=nums[end];
nums[end]=tmp;
++start;
--end;
}
} void rotate(int* nums, int numsSize, int k) {
k=k%numsSize;
if (k==0) return;
reverse(nums,0,numsSize-k-1);
reverse(nums,numsSize-k,numsSize-1);
reverse(nums,0,numsSize-1);
}
leetcode:Rotate Array的更多相关文章
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- Java [Leetcode 189]Rotate Array
题目描述: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...
- C#解leetcode 189. Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode(67)-Rotate Array
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- LeetCode之“数组”:Rotate Array
题目链接 题目要求: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, ...
- LeetCode OJ:Rotate Array(倒置数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- leetcode解题报告(20):Rotate Array
描述 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- LeetCode 189. Rotate Array (旋转数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
随机推荐
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 2014ACM/ICPC亚洲区鞍山站 清华命题
A http://acm.hdu.edu.cn/showproblem.php?pid=5070 先跳过. B http://acm.hdu.edu.cn/showproblem.php?pid=50 ...
- UML教程首页(转载)
UML是一种标准语言,用于指定,可视化,构造和文档的软件系统的文物. UML是OMG在1997年1月提出了创建由对象管理组和UML1.0规范草案. 本教程给出了一个比较完整的学习理解UML,可以方便学 ...
- javascript中关于坐标 大小 的描述
window对象 有效桌面的大小,除去桌面下面的任务栏的高度 window.screen.availHeight : window.screen.availWidth : 浏览器窗口的左上角相对于 ...
- android 关于Location of the Android SDK has not been setup in the preferences的解决方法
今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...
- java基础类:Object类和Math类
1.2.3.4.5.6.7.7.
- HDU4945 2048(dp)
先是看错题意..然后知道题意之后写了发dp..无限TLE..实在是不知道怎么优化了,跑了遍数据是对的,就当作理论AC掉好了.. #pragma warning(disable:4996) #inclu ...
- line-height 与垂直居中!
在此之前,对于line-height 与垂直居中的问题,经常碰到. 比如,图片与span在同一个box中的时候,竟然会各种偏移.要想达到理想的效果真的是各种难. 有时间,决定认真的啃一啃. 一 lin ...
- ActionResult 返回类型
类名 抽象类 父类 功能 ContentResult 根据内容的类型和编码,数据内容. EmptyResult 空方法. FileResult abstract 写入文件内容,具体 ...
- Webpack+React配合开发
前面两篇关于webpack的基础和进阶,请先务必阅读之前的文章. Webpack教程一 Webpack教程二 什么是React React是一个由Facebook开发的library,它的口号是“A ...