(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].

题目:

给一数组,n个元素,将数组向右移动循环移动k个元素。

思路:

注意题目的要求是循环移动,所以k可以是任意非负整数,但数组元素个数为n,因此k=k%n。

一种通用的思路就是:

1、翻转整个数组 :A[0…n-1];如[7,6,5,4,3,2,1]

2、翻转数组前k个:A[0,k-1];如[5,6,7,4,3,2,1]

3、翻转数组后n-k个:A[k,n-1];如[5,6,7,1,2,3,4]

类似的应用:翻转句子中的全部单词,单词内容不变。

代码:

class Solution {
public:
    void reverse(int nums[],int i,int j){
        while(i<j){
            int tmp=nums[i];
            nums[i]=nums[j];
            nums[j]=tmp;
            i++;
            j--;
        }
    }

    void rotate(int nums[], int n, int k) {
        k=k%n;
        if(k>=1 && n>1 && k<=n){
            reverse(nums,0,n-1);
            reverse(nums,0,k-1);
            reverse(nums,k,n-1);
        }
    }
};
时间: 2024-10-08 00:04:03

(LeetCode 189)Rotate Array的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

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

Leetcode解题笔记-Rotate Array

题目要求: 将数组的位置向右旋转移动k位. 个人解法: 1.这个题目重点在于需要对边界进行判断,因为k的值有的时候会比n要大,所以要充分理解%的意义 2.定义一个copynums可以存储临时变量. 3. 在第二个循环中可以利用(i+k)%len的公式准确找到并赋值需要变化的位置. 相关代码: public void rotate(int[] nums, int k) { int len = nums.length; if(len==0) return; int[] copynums = new

(leetcode题解)Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

ZigZag问题的一种新思路(Leetcode #6)

原始问题(leetcode ZigZag Conversion) 如果固定一个行数(这里是3),字符串 "PAYPALISHIRING" 用"之"字形的方式可以写成: P A H N A P L S I I G Y I R 这个结果按照一行一行访问的话是: "PAHNAPLSIIGYIR".给定一个字符串以及一个表示行数的整数,实现一个函数,返回按行序拼接而成的新串. string convert(string text, int nRows);

(LeetCode 78)SubSets

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. 题目要求 :求整数数组的所有子集 注意: 1.子集元素按非降序排列 2.不包含重复的子集 解题思路: 求解这类诸如子集的题目,都可以采用回溯法

(LeetCode 72)Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

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 [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 将数组的内容倒置,看例子就知道是什么意思: 1 class Solution { 2 public: 3 void rotate(vector<int>& nums, int k) { 4 if(

(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 pr