LeetCode_189. Rotate Array
189. Rotate Array
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的更多相关文章
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- 理解JavaScript中的参数传递 - leetcode189. Rotate Array
1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...
- 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 ...
- Leetcode-189 Rotate Array
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 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 ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 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 = ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- 《hello--world团队》第四次作业:项目需求调研与分析
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验八 团队作业4:基于原型的团队项目需求调研与分析 团队名称 <hello--w ...
- Git的撤销操作
https://blog.csdn.net/qq_36431213/article/details/78858848 Git 初接触 (三) Git的撤销操作 git reset HEAD -- gi ...
- 52、[源码]-Spring源码总结
52.[源码]-Spring源码总结 总结 一.Spring容器在启动的时候,先会保存所有注册进来的Bean的定义信息: xml注册bean: 注解注册Bean:@Service.@Component ...
- Update和Select结合统计更新
Update和Select结合统计更新 update table_a set updatetime=getdate(), name=b.name from (select name,age from ...
- Spring动态代理及Spring Bean的生命周期
数组添加值 public class DiTest { /** * 数组 */ private String [] arrays; /** * List:集合 */ private List<I ...
- react页面跳转 window.location.href和window.open的几种用法和区别
https://www.cnblogs.com/Qian123/p/5345298.html
- Cogs 461. [网络流24题] 餐巾(费用流)
[网络流24题] 餐巾 ★★★ 输入文件:napkin.in 输出文件:napkin.out 简单对比 时间限制:5 s 内存限制:128 MB [问题描述] 一个餐厅在相继的N天里,第i天需要Ri块 ...
- 从输入URL到浏览页面的过程
之前我们已经讨论过浏览器的渲染原理,今天我们来讨论下更广泛的从输入URL到渲染出页面的过程. 1. 查询该URL是否有缓存 如果有,则直接返回,没有的话,下一步 2. 查询URL对应的IP 首先,到 ...
- FOI冬令营 Day2
目录 T1.直径(diameter) 传送门 Code T2.定价(price) 传送门 Code T3.排序(sort) 传送门 Code T1.直径(diameter) 传送门 Code //20 ...
- vmware如何克隆多个linux系统
安装一次系统相对来说耗时较长,且还要做各种配置,那么克隆就不失为一种好的选择.接下来我把我做系统克隆的步骤写下来,供大家参考: 右键点击已经安装的虚拟机,选择管理-->克隆,接下来弹出一个窗口 ...