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

Related problem: Reverse Words in a String II

Credits:

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

Subscribe to see which companies asked this question

解题分析:

对于这个可以利用分治方法,可以利用给出的 k 值将数列分成两部分,

分别对其中的每一部分进行逆置,然后再把整个逆置一下。

实现逆置的算法原理:

a ^= b

b ^= a

a ^= b

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def rotate(self, nums, k):
        n = len(nums)
        k %= n
        self.reverse(nums, 0, n - k)
        self.reverse(nums, n - k, n)
        self.reverse(nums, 0, n)
    def reverse(self, nums, start, end):
        for x in range(start, (start + end) / 2):
            nums[x] ^= nums[start + end - x - 1]
            nums[start + end - x - 1] ^= nums[x]
            nums[x] ^= nums[start + end - x - 1]
时间: 2024-11-03 03:02:52

(LeetCode)Rotate Array --- 逆置数组的相关文章

C 求最大数,逆置数组 冒泡法

求第一,第二,第三大的值 #include <stdio.h> int main(){     int arr1[10]={1,3,2,5,4,7,5,6,9};      int max        =0;     int second_max =0;     int third_max  =0; for(int a=0;a<10;a++){ if(arr1[a] > max){ third_max  =second_max; second_max =max; max     

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

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思路: 思路一:新建数组,复制原

表逆置[数组和链表]

对于数组(也可为线性表),逆置就是首尾数据两两交换,首先计算交换的次数: 中间需要一个临时变量暂存数据,看似简单,其实有点绕,关键还是数组下标从0开始,这一点很麻烦!这种小题最练基础! 完整代码: #include <iostream> using namespace std; int main() { int a[]={1,2,3,4,5,7,8,9}; int len=sizeof(a)/sizeof(int);/*计算数组长度*/ int temp;/*临时变量*/ for(int i=

leetcode——189 Rotate Array Total(数组的翻转)

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

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 pro

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

翻译 通过K步将一个有着n个元素的数组旋转到右侧. 例如, 给定n = 7和k = 3,数组[1,2,3,4,5,6,7]会被旋转成[5,6,7,1,2,3,4]. 批注: 尽你可能尝试多种解决方案,这里至少存在3种不同的方式去解决这个问题. 原文 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 rotate

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