Jan 07 - Rotate Array

public class Solution {
public void rotate(int[] nums, int k) {
if(nums.length < 2) return;
k = k % nums.length;
if(k == 0) return;
int tail = 0;
for(int i = 1; i <= k; i++){
tail = nums[nums.length-1];
for(int j = 1; j < nums.length; j++){
nums[j] = nums[j-1];
}
nums[0] = tail;
}
}
}

Time Limit Exceeded!!!

看来不能用brute force,想想其他方法吧。

public class Solution {
public void rotate(int[] nums, int k) {
if(nums.length < 2) return;
k = k % nums.length;
if(k == 0) return;
int count = 0;
int i = 0;
while(count < nums.length){
count += switchPos(i, k, nums);
i++;
}
}

public int switchPos(int i, int k, int[] nums){
int temp = nums[i];
int start = i;
int count = 0;
while((i-k)!=start){
if(i < k){
nums[i] = nums[nums.length+i-k];
i = nums.length+i-k;
}
else{
nums[i] = nums[i-k];
i = i - k;
}
count++;
}
nums[i] = temp;
return ++count;
}
}

Runtime: O(n)

时间: 2024-08-04 20:40:04

Jan 07 - Rotate Array的相关文章

[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 [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

189. Rotate Array

1. 问题描述 189. Rotate ArrayRotate 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

LeetCode Rotate Array 翻转数组

题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. 1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 if( !k || !n || n==1 || k==n ) return; 5 k %= n; 6 vector<int> cha; 7 cha.reserve

【LeetCode】189. Rotate Array

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 s

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

151. Reverse Words in a String Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

leetCode 189. Rotate Array 数组

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 [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

189. Rotate Array【easy】

189. Rotate Array[easy] 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 differen

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

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]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

LeetCode:(Array-189) Rotate Array

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 s