Question

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

Solution 1 Bubble Rotate

Similar with bubble sort, time complexity O(k * n), space cost O(1)

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

Solution 2 Traverse

Key to this solution is to make a copy of original array. Time complexity O(n), space cost O(n).

Solution 3 Reversal

Assuming we are given {1,2,3,4,5,6} and order 2. The basic idea is:

1. Divide the array two parts: 1,2,3,4 and 5, 6

2. Rotate first part: 4,3,2,1,5,6

3. Rotate second part: 4,3,2,1,6,5

4. Rotate the whole array: 5,6,1,2,3,4

Time complexity O(n), space cost O(n)

 public class Solution {
public void rotate(int[] nums, int k) {
int length = nums.length;
if (k >= length)
k = k % length;
if (k == 0)
return;
reverse(nums, 0, length - k - 1);
reverse(nums, length - k, length - 1);
reverse(nums, 0, length - 1);
}
private void reverse(int[] nums, int start, int end) {
if (start == end || nums.length == 1)
return;
while (start < end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
}

Rotate Array 解答的更多相关文章

  1. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  2. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  3. LeetCode189——Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. Leetcode-189 Rotate Array

    #189.    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...

  5. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  6. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  7. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  8. 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 = ...

  9. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. POJ1664(简单动态规划)

    #include<iostream> #include<string> #include<cstring> using namespace std; ][]; vo ...

  2. 【HDU3371】Connect the Cities(MST基础题)

    注意输入的数据分别是做什么的就好.还有,以下代码用C++交可以过,而且是500+ms,但是用g++就会TLE,很奇怪. #include <iostream> #include <c ...

  3. MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型

    为ASP.NET MVC程序创建Entity Framework数据模型 (1 of 10) By  Tom Dykstra |July 30, 2013 Translated by litdwg   ...

  4. python list 按长度分段

    def changes(a,b): #list 分段函数,a:数据[(1),(2)]b:长度 for i in xrange(0,len(a),b): yield  a[i:i+b] for i in ...

  5. deciaml(十进制浮点运算)

    # -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...

  6. 创建UILabel

    UILabelCreate.h #import <UIKit/UIKit.h> @interface UILabelCreate : UILabel /** * 创建UILabel 初始化 ...

  7. 用ajaxFileUpLoad上传文件不能正确取得返回值的问题

    刚開始没有认为ajax请求的dataType參数的重要性,用了ajaxFileUpLoad插件后,假设页面代码例如以下: fileElementId : ['imageToUpload'], url ...

  8. Git 笔记二-Git安装与初始配置

    git 笔记二-Git安装与初始配置 Git的安装 由于我日常生活和工作基本上都是在Windows上,因此此处只说windows上的安装.Windows上的安装和其他程序一样,只需要到http://g ...

  9. CentOS6.5 --安装orale 11g(上)

    Linux内核版本:Linux version 2.6.32-431.23.3.el6.x86_64 (1)     在Windows上安装Xmanager Enterprise 4工具,该工具是用来 ...

  10. 整合 新浪 腾讯 人人 qq空间 分享地址

    function snsShare(snsId, title, content, image, url) { var snsUrl; // 新浪 腾讯 要申请appkey switch (snsId) ...