Leetcode 数组问题3:旋转数组
问题描述:
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
示例 :
输入A数组:[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]尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。要求使用空间复杂度为 O(1) 的原地算法。
问题分析:
将数组中的元素向右移动,并要求空间复杂度为O(1),每移动一次,数组的最后一个元素移动到首位,其余元素均向后移动一位,
以上面的A数组为例,可以将A[6]=7先拿出来,不被向后移动的其他数组元素的值覆盖掉,然后将其余元素向后移动一位,数组变为[A[0],1,2,3,4,5,6],再把
A[6]的值赋给A[0],这样就完成了一次数组的移动,循环上述过程即可。这种方法时间复杂度为O(KN),比较耗时
翻转算法:先将数组整个翻转一遍,将后面的元素移动到了前面,但是这时的元素顺序并不满足要求,需要将索引为[0,k-1],和 [k,n-1]的元素翻转回来
时间复杂度为O(n)
以上面的A数组为例:
[1,2,3,4,5,6,7] --翻转索引为[0,n-1]之间的元素--> [7,6,5,4,3,2,1]
--翻转索引为[0,k-1]之间的元素--> [5,6,7,4,3,2,1]
--翻转索引为[k,n-1]之间的元素--> [5,6,7,1,2,3,4]
JAVA实现:
class Solution {
public void rotate(int[] nums, int k) {
int len=nums.length;
k=k%len;//移动的长度只需要对原数组长度取余即可,当k=len时,数组不变
int temp=0;
if(k==0){}
else{
for(int j=0;j<k;j++){
temp=nums[len-1];//先将数组最后一个元素的值赋给temp
for(int i=len-2;i>=0;i--){
nums[i+1]=nums[i];//剩余数组元素均向右移动一位
}
nums[0]=temp;//数组最后一个元素移动到第一个元素的位置
}
}
}
}

class Solution {
//翻转算法
public void rotate(int[] nums, int k) {
int n = nums.length;
k = k%n;
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}
private void reverse(int[] nums, int i, int j) {
while (i< j) {
int temp = nums[i];
nums[i++] = nums[j];
nums[j--] = temp;
}
}
}

Leetcode 数组问题3:旋转数组的更多相关文章
- 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 旋转数组
旋转数组 题目地址:https://leetcode-cn.com/problems/rotate-array/ 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输 ...
- [LEETCODE] 初级算法/数组 1.3旋转数组
原题: 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右 ...
- 剑指offer 旋转数组
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { //常规的遍历方法时间是O(N ...
- 剑指offer编程题Java实现——面试题8旋转数组的最小数字
剑指offer面试题8:旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1, ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- [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]解释 ...
随机推荐
- str 方法总结整理
#!/usr/bin/env python #Python 3.7.0 字符串常用方法 __author__ = "lrtao2010" #capitalize 将字符串的首字符改 ...
- Applied Nonparametric Statistics-lec1
参考网址: https://onlinecourses.science.psu.edu/stat464/node/2 Binomial Distribution Normal Distribution ...
- 水题:UVa213- Message Decoding
Message Decoding Some message encoding schemes require that an encoded message be sent in two parts. ...
- selenium2基本控件介绍及其代码
输入框:input 表现形式: 1.在html中一般为:<input id="user" type="text"> 主要操作: ...
- App架构经验总结
作者:李纪钢,网名 Keegan小钢,博客地址:http://keeganlee.me.目前在广州日报新媒体有限公司,负责移动产品的研发工作. 关于:本文整理自CSDN架构主题月子活动金牌架构师微课堂 ...
- HDU 3727 Jewel 主席树
题意: 一开始有一个空序列,然后有下面四种操作: Insert x在序列尾部加入一个值为\(x\)的元素,而且保证序列中每个元素都互不相同. Query_1 s t k查询区间\([s,t]\)中第\ ...
- meta-data
<meta-data android:name="string" android:resource="resource specification" ...
- A+B问题的异常解法
先%XZZ为敬 http://www.cnblogs.com/xzz_233/p/a-plus-b-problem.html 万恶之源:https://www.luogu.org/discuss/sh ...
- Linux中的more命令
ore命令,功能类似 cat , cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,而且还有搜寻 ...
- PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述
本文简要介绍 zend 虚拟机解释执行字节码的基本逻辑以及相关的数据结构,关于 PHP 源代码的下载,编译,调试可以参考之前的系列文章 execute_ex 我们来看看执行一个简单的脚本 test.p ...