最近看了一道题,自己做个过后又参考了网上的解法,为了加深对这个解法的理解和记忆于是有了这篇博客,供自己以后复习用

题目:

给定一个数组,将数组中的元素向右移动 个位置,其中 是非负数。

示例 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) 的 原地 算法。

解法:

 var rotate = function(nums, k) {
var temp1, temp2, index, count = 0; // temp1存要换到后边的值,temp2存后边的值
var len = nums.length
k %= len
if (k !== 0 ) {
for (var i = 0; i <= k; i++) { // 为什么最多k轮,因为照这种方法一个一个挪最多就是k轮,再挪就是挪挪过的元素了
if (count >= len) break;
index = i;
temp1 = nums[i];
while((index + k) % len !== i) { // i在while出来之前一直没变,所以当 (index + k) % len === i 时,把从里面出来的temp1给nums[i],如17行
temp2 = nums[(index + k) % len]
nums[(index + k) % len] = temp1
index = (index + k) % len
temp1 = temp2
count++
} // 只有当数组中要换到后面的那个元素的下标===开始循环是的下标的时候跳出循环
nums[i] = temp1
count++
}
console.log(nums)
}
};
rotate([1, 2, 3, 4, 5, 6, 7], 3)

LeetCode:旋转数组的更多相关文章

  1. leetcode旋转数组查找 二分查找的变形

    http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivo ...

  2. (LeetCode)旋转数组

    原体描写叙述例如以下: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3 ...

  3. [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  ...

  4. 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  ...

  5. 【Leetcode】【简单】【189. 旋转数组】【JavaScript】

    189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释 ...

  6. LeetCode初级算法--数组02:旋转数组

    LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...

  7. [LeetCode] 189. Rotate Array 旋转数组

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

  8. 前端与算法 leetcode 189. 旋转数组

    目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...

  9. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  10. 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...

随机推荐

  1. 转 router-view 的理解

    主要是构建 SPA (单页应用) 时,方便渲染你指定路由对应的组件.你可以 router-view 当做是一个容器,它渲染的组件是你使用 vue-router 指定的.比如: 视图层: <div ...

  2. Codeforces 1082G(最大权闭合子图)

    题面 传送门 分析 没想到压轴题是道模板裸题 由于子图的权值=边权和-点权和 将边和点都看成新图中的点 S向原来的边i连边,权值为边权 点i向T连边,权值为点权 边i:(u,v,w)向u,v,连边,权 ...

  3. hdu4352 XHXJ's LIS(数位dp)

    题目传送门 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 基于Airtest实现python自动抢红包

    一.Airtest的介绍 1)Airtest的简介 Airtest是网易出品的一款基于图像识别和poco控件识别的一款UI自动化测试工具. Airtest的框架是网易团队自己开发的一个图像识别框架,这 ...

  5. 修改Oracle并行度

    什么是并行度: 并行度的优点就是能够最大限度的利用机器的多个cpu资源,是多个cpu同时工作,从而达到提高数据库工作效率的目的.在系统空闲时间,使用并行是个不错的选择,但是好东西总是相对而言,没有绝对 ...

  6. 解决Minikube start卡住的方法

    安装与问题 在mac上安装minikube对k8s进行学习,根据官方Quick Start brew cask install minikube 就可以完成minikube的安装 在安装前需要安装vi ...

  7. Wait and Click Element

    Wait and Click Element [Documentation] 等待元素出现并单击元素 [Arguments] ${locator} Wait Until Element Is Visi ...

  8. .net core 添加NLog

    依赖项——右键——管理NuGet程序包——浏览——输入以下内容 Install-Package NLog.Extensions.Logging -Pre 在根目录下添加nlog.config   更改 ...

  9. vue的列表交错过渡

    参考文章 https://juejin.im/post/5cccf5b0e51d453a907b4af1

  10. ThinkingRock:使用方法

    摘自:http://www.mifengtd.cn/articles/how_to_use_thinkingrock.html 不使用Thinkingrock的朋友,也可以看看.因为在处理(Proce ...