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

[show hint]

Hint:
Could you do it in-place with O(1) extra space?

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

新题抢先刷,这道题标为Easy,应该不是很难,题目明确要求了空间复杂度为O(1),那么肯定不能建立新的数组,所以只能覆盖原数组的值,仔细观察题目中给的那个例子,k = 3说是移动了三步,怎么移的呢,原来是每次把最末尾的数移动到开头,移三次就得到最终结果。代码如下:

class Solution {
public:
    void rotate(int nums[], int n, int k) {
        for (int i = 0; i < k; ++i) {
            moveRight(nums, n);
        }
    }
    void moveRight(int nums[], int n) {
        if (n == 0) return;
        int last = nums[n - 1];
        for (int i = n - 1; i >= 1; --i) nums[i] = nums[i - 1];
        nums[0] = last;
    }
};

题目说至少有三种解法,暂时只想到一种,容我慢慢想想~~

时间: 2024-08-05 09:58:11

[LeetCode] Rotate Array 旋转数组的相关文章

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]12. 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 pro

rotate array 旋转数组

class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0; /* //------------------- //解法一  会超时 //-------------------- k=k%n; while(i<k){ int temp=nums[n-1]; for(int j=n-1;j>0;j--){ nums[j]=nums[j-1]; } nums

[LeetCode] Rotate List 旋转链表

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 这道旋转链表的题和之前那道Rotate Array 旋转数组 很类似,但是比那道要难一些,因为链表的值不能通过下表来访问,只能一个一个的

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

[LeetCode]25. Search in Rotated Array旋转数组查找

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

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 189:旋转数组 Rotate Array

公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the right by k steps, where k is non-negative. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,

Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

O(n)的算法就不说了,这题主要考查的是 O(logn)的算法. 有序数组容易想到使用二分查找解决,这题就是在二分基础上做一些调整.数组只有一次翻转,可以知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题只需要考虑有序数组的递增情况). 假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n].则 A[0..x] 这一段是有序递增的, A[x+1..n] 这一段也是有序递增的.并且因为原数组是有序递增的,A[0..x] 中所有数都会大于 A[x+1..n] 中