旋转数组 [ LeetCode ]
原题地址:https://leetcode-cn.com/problems/rotate-array/description/
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
示例 1:
输入: [1,2,3,4,5,6,7] 和 k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右旋转 1 步: [7,1,2,3,4,5,6]
向右旋转 2 步: [6,7,1,2,3,4,5]
向右旋转 3 步: [5,6,7,1,2,3,4]
示例 2:
输入: [-1,-100,3,99] 和 k = 2
输出: [3,99,-1,-100]
解释:
向右旋转 1 步: [99,-1,-100,3]
向右旋转 2 步: [3,99,-1,-100]
说明:
- 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
- 要求使用空间复杂度为 O(1) 的原地算法。
以上是原题
这道算法题目本身并不难,但要求使用三种不同方法解决问题,这就考验小伙伴们脑洞能开多大了。。。
解法一:
首先用最普通的解法,根据题目描述,我们不难得出,将一个元素右移k个位置,原处于index位置的元素会出现在index + k 的位置上,如果index + k 超出数组长度,那么从0开始接着移动,新的下标等价于 (index + k) % length.
直接上代码:
//旋转数组
public void rotate(int[] nums, int k) {
int[] temp = Arrays.copyOf(nums, nums.length);
for (int index = 0; index < temp.length; index++) {
nums[(index + k) % temp.length] = temp[index];
}
}
这种解法简单粗暴,效率也不错,但其实题目中要求尽量使用空间复杂度为O(1)的原地算法,上述解法使用了一个与输入数组同样大小的空间。
解法二:
移动第一个元素到k位置,将原本处在k位置的元素向右移动k位置,如果遇到了超出数组长度且指针指向数组第0个位置时,指针后移一位。
public void rotate(int[] nums, int k) {
if (nums.length == 0 || (k %= nums.length) == 0) {
return;
}
int length = nums.length;
int start = 0, index = 0, cnt = 0, temp = 0;
int cur = nums[index];
while (cnt++ < length) {
index = (index + k) % length;
temp = nums[index];
nums[index] = cur;
if (index == start) { //是否循环到已处理过的元素
++start;
cur = nums[++index];
} else {
cur = temp;
}
}
}
执行该方法时,数组状态变化如下:
测试数据: [2, 7, 11, 15, 12, 16] 向右移动2位
2 7 15 12 16
2 7 2 15 16
7 2 15 11 16
12 7 2 11 16
12 7 2 7 11
12 2 7 11 15
解法三:
这种方法比较特别,将原有数组用 length - k 分割为两段,分别对这两段中的数字进行首尾换位,然后再对整个数组进行首尾换位。
public void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - 1 - k);
reverse(nums, nums.length - k, nums.length - 1);
reverse(nums, 0, nums.length - 1);
}
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int tmp = nums[start];
nums[start++] = nums[end];
nums[end--] = tmp;
}
}
同样给出数据状态:
测试数据: [2, 7, 11, 15, 12, 16] 向右移动2位
15 11 7 2 12 16 //第一次reverse
15 11 7 2 16 12 //第二次reverse
12 16 2 7 11 15 //第三次reverse
坦白说,这个方法我没想明白什么原理,但的确很神奇,如果有朋友明白里的原理,还请不吝赐教啊。。。。。
旋转数组 [ LeetCode ]的更多相关文章
- [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 ...
- 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 ...
- 【Leetcode】【简单】【189. 旋转数组】【JavaScript】
189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释 ...
- LeetCode初级算法--数组02:旋转数组
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 前端与算法 leetcode 189. 旋转数组
目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...
- Leetcode 153.寻找旋转数组中的最小值
寻找旋转数组中的最小值 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. ...
随机推荐
- linux课后作业1
本实验6第一题:菜单驱动程序. 随便进到某个目录,vim driver.sh 把代码写进去. #!/bin/bash function welcome() { echo -e "\n&quo ...
- luogu4238 【模板】多项式求逆
ref #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...
- **leetcode笔记--4 Sum of Two Integers
question: Calculate the sum of two integers a and b, but you are not allowed to use the operator + a ...
- Anytime项目开发记录2
注册,登陆于密码找回.这是这次记录的主要内容. 首先,我们来看类图: 因为一直在改,所以与第二篇介绍项目框架时的图会有一些不一样. 代码都是非常简单的. 由于在注册和登陆这里,需要弹出一些对话框告诉用 ...
- C#调用C++编写的dll
界面还是C#写的方便点,主要是有一个可视化的编辑器,不想画太多的时间在界面上.但是自己又对C++了解的多一些,所以在需要一个良好的界面的情况下,使用C++来写代码逻辑,将其编译成一个dll,然后用C# ...
- jsp 路径问题和环境路径以及各种路径总结
首先确定问题: 浏览器发送请求后,服务器会返回一个响应,但是返回的网页中,会有各种路径问题,所以在此用jsp中的属性来解决.(只是记录问题,用了不专业的术语,请见谅.) 总结: 以路径 http:/ ...
- 第二篇 Python初识别及变量名定义规范
第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是 .py 两种python程 ...
- resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)
#!/usr/bin/python import os import json import subprocess from cloudinit.sources.DataSourceConfigDri ...
- 维特比算法(Viterbi)及python实现样例
维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...
- Week9 Teamework from Z.XML-Scenario testing
关于场景测试 About Scenario testing 一.关于用户(About Personas) 1 我们如何预期我们的用户对我们的软件的使用 (How do we expect diff ...