189. Rotate Array

Easy

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?
package leetcode.easy;

public class RotateArray {
private static void print_arr(int[] nums) {
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
} public void rotate1(int[] nums, int k) {
int temp, previous;
for (int i = 0; i < k; i++) {
previous = nums[nums.length - 1];
for (int j = 0; j < nums.length; j++) {
temp = nums[j];
nums[j] = previous;
previous = temp;
}
}
} public void rotate2(int[] nums, int k) {
int[] a = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
a[(i + k) % nums.length] = nums[i];
}
for (int i = 0; i < nums.length; i++) {
nums[i] = a[i];
}
} public void rotate3(int[] nums, int k) {
k = k % nums.length;
int count = 0;
for (int start = 0; count < nums.length; start++) {
int current = start;
int prev = nums[start];
do {
int next = (current + k) % nums.length;
int temp = nums[next];
nums[next] = prev;
prev = temp;
current = next;
count++;
} while (start != current);
}
} public void rotate4(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
} public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
} @org.junit.Test
public void test1() {
int[] nums1 = { 1, 2, 3, 4, 5, 6, 7 };
int k1 = 3;
int[] nums2 = { -1, -100, 3, 99 };
int k2 = 2;
print_arr(nums1);
rotate1(nums1, k1);
print_arr(nums1); print_arr(nums2);
rotate1(nums2, k2);
print_arr(nums2);
} @org.junit.Test
public void test2() {
int[] nums1 = { 1, 2, 3, 4, 5, 6, 7 };
int k1 = 3;
int[] nums2 = { -1, -100, 3, 99 };
int k2 = 2;
print_arr(nums1);
rotate2(nums1, k1);
print_arr(nums1); print_arr(nums2);
rotate2(nums2, k2);
print_arr(nums2);
} @org.junit.Test
public void test3() {
int[] nums1 = { 1, 2, 3, 4, 5, 6, 7 };
int k1 = 3;
int[] nums2 = { -1, -100, 3, 99 };
int k2 = 2;
print_arr(nums1);
rotate3(nums1, k1);
print_arr(nums1); print_arr(nums2);
rotate3(nums2, k2);
print_arr(nums2);
} @org.junit.Test
public void test4() {
int[] nums1 = { 1, 2, 3, 4, 5, 6, 7 };
int k1 = 3;
int[] nums2 = { -1, -100, 3, 99 };
int k2 = 2;
print_arr(nums1);
rotate4(nums1, k1);
print_arr(nums1); print_arr(nums2);
rotate4(nums2, k2);
print_arr(nums2);
}
}

LeetCode_189. 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. MySql 数据库 SQLException: The user specified as a definer ('root'@'%') does not exist 错误原因及解决办法

    The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 经查明:是用户root并没有获 ...

  2. Jmeter与搜狗输入法、百度输入法不兼容问题解决方法

    问题:Jmeter 3.3.4.0等版本与最新版搜狗输入法.百度输入法不兼容,输入法悬浮窗口无法实时显示出来. 解决方法:换用微软拼音输入法即可. 微软拼音输入法: 官方下载链接: https://w ...

  3. oracle存储过程把查询到的值更新到别的表

    create or replace procedure update_nst_t_Clime2 as cursor c_db is select * from NST_T_FRAME f ,) as ...

  4. OpenCV:Python下OpenCV安装和入门最强详细攻略

    一.关于OpenCV简介       OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效— ...

  5. 关于解决ruby源码安装 gem install报错问题

    因做redis集群需要安装ruby,源码安装过后gem install redis安装redis接口报错 解决方案: 确保主机安装zlib,没有安装执行 yum -y install zlib zli ...

  6. ssh sshpass随笔

    1: 当通过ssh连接远程服务器的时候,可能会出现以下繁琐场景,需要手工输入yes: ssh username@ip 这对于某些分布式集群来说是不行的,甚至导致集群都不能启动成功,对于像pssh,ps ...

  7. 配置centos7阿里镜像源和epel源

    [root@runstone yum.repos.d]# pwd /etc/yum.repos.d [root@runstone yum.repos.d]# cat aliBase.repo #镜像源 ...

  8. 线程wait和notify方法的demo详解

    wait和notify都是针对某个线程而言的: package com.roocon.thread.t1; public class NewThread implements Runnable { @ ...

  9. BZOJ1941Hide and Seek

    做KD_tree的入门题. 问题就是求出任意一个点距其他点的最大曼哈顿距离和最小曼哈顿距离差,然后对其取min即可. 这个东西就是KD_tree可以轻松解决的了. 下面总结一下做KD_tree(不带修 ...

  10. Centos-7修改yum源(阿里yum源)

    国外地址yum源下载慢,下到一半就断了,就这个原因就修改它为国内yum源地址 国内yum源: 阿里centos7 yum源:http://mirrors.aliyun.com/repo/Centos- ...