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. 使用Scrapy框架爬取腾讯新闻

    昨晚没事写的爬取腾讯新闻代码,在此贴出,可以参考完善. # -*- coding: utf-8 -*- import json from scrapy import Spider from scrap ...

  2. RocketMQ的技术亮点

    高性能 存储原理 零拷贝 数据结构与存储逻辑 刷盘策略 长轮询PULL RocketMQ的Consumer都是从Broker拉消息来消费,但是为了能做到实时收消息,RocketMQ使用长轮询方式,可以 ...

  3. Nginx 负载均衡条件下 Tomcat 共享Session (Java)(一)

    1.修改tomcat 下 conf/context.xml  在</Context>里面加入以下代码 <Valve className="com.orangefunctio ...

  4. PostgreSQL 数据目录结构

    根目录介绍 data ├── base # use to store database file(SELECT oid, datname FROM pg_database;) ├── global # ...

  5. string字符串类型用scanf读入,printf输出

    #include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

  6. 认识WebStorm-小程序框架wepy

    WebStorm是一个功能强大的IDE,适用于JavaScript开发,适合使用Node.js进行复杂的客户端开发和服务器端开发. WebStorm具有对JavaScript,HTML, CSS及其现 ...

  7. P3986 斐波那契数列——数学(EXGCD)

    https://www.luogu.org/problem/P3986 很久很久以前,我好像写过exgcd,但是我已经忘了: 洛谷上搜EXGCD搜不到,要搜(扩展欧几里得) 这道题就是ax+by=k, ...

  8. OpenJudge 1.5.27:级数求和

    描述 已知:Sn= 1+1/2+1/3+…+1/n.显然对于任意一个整数K,当n足够大的时候,Sn大于K. 现给出一个整数K(1<=k<=15),要求计算出一个最小的n:使得Sn>K ...

  9. WPF_AutoCompleteBox智能提示_Xml读写

    效果图 1.需要引用的DLL 2. Window.xaml <Window x:Class="自己的命名空间" xmlns="http://schemas.micr ...

  10. [WEB安全]代码/命令执行总结

    0x01 代码执行 1.1 概念 远程代码执行实际上是调用服务器网站代码进行执行. 1.2 常见执行方法 eval eval():将字符串当做函数进行执行(需要传入一个完整的语句) demo: < ...