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 ...
随机推荐
- 【CodeForces】【338E】Optimize!
线段树 先搞出来每个a[i]能连多少条边记为w[i]……如果对一组s[i],都满足w[i]-rank[i]>=0则这是一组合法方案,然后线段树维护w[i]-rank[i](第一个元素出去的时候后 ...
- 【ASP.Net MVC】在AspNet Mvc使用JQuery AutoComplete组件
在AspNet Mvc使用JQuery AutoComplete组件 官方文档: http://api.jqueryui.com/autocomplete/#entry-examples 要使用JQu ...
- Codeforces Round #268 (Div. 2)
补题解: E:只会第四种解法:也只看懂了这一种. PS:F[X+10^18]=F[X]+1;F[X]表示X的数字之和; 假设X,F[10^18+X]+F[10^18+X-1]+......F[10^1 ...
- Codeforces Round #253 (Div. 2) D题
题目大意是选出一个其他不选,问问最大概率: 刚开始想到DP:F[I][J][0]:表示从 前I个中选出J个的最大值, 然后对于F[I][J][1]=MAX(F[I-1][J][1],F[I-1][J- ...
- HDOJ 3183 A Magic Lamp
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【五】PHP数组操作函数
1.输出数组的结构:bool print_r(数组); $arr=array('jack','mike','tom'); print_r($arr);//Array ( [0] => jack ...
- **app后端设计(10)--数据增量更新(省流量)
在新浪微博的app中,从别的页面进入主页,在没有网络的情况下,首页中的已经收到的微博还是能显示的,这显然是把相关的数据存储在app本地. 使用数据的app本地存储,能减少网络的流量,同时极大提高了用户 ...
- ExtJs之 Ext.JSON
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- UVA 12373 Pair of Touching Circles
思路:(注意2个圆的半径可以不一样) 有2种情况: 1) 水平和竖直放.这种情况很简单,刚开始以为只有这种情况,但是样例5不对,后来知道还有一种情况. 2)斜线也可以放.只要满足勾股数就可以.现在的问 ...
- poj 1568 Find the Winning Move 极大极小搜索
思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...