LeetCode:旋转数组
最近看了一道题,自己做个过后又参考了网上的解法,为了加深对这个解法的理解和记忆于是有了这篇博客,供自己以后复习用
题目:
给定一个数组,将数组中的元素向右移动 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) 的 原地 算法。
解法:
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:旋转数组的更多相关文章
- leetcode旋转数组查找 二分查找的变形
http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivo ...
- (LeetCode)旋转数组
原体描写叙述例如以下: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 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 ...
- 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., ...
随机推荐
- BZOJ1672 Cleaning Shifts 清理牛棚
传送门 显然可以考虑 $dp$ 设 $f[i]$ 表示当前到了时间 $i$,从初始到 $i$ 的时间都安排好打扫了 把所有牛按照区间 $l,r$ 双关键字排序 这样枚举到一头牛 $x$ 时,在 $x. ...
- GeneXus笔记本—GeneXusIDE如何切换成中文语言
嘛 有些人可能比较习惯英文IDE,但是有些人就比较难受 所以为了应对各个地区的差异 GeneXus很人性化的自带了一部分国家的语言包 只不过默认是英文 需要改动一下_(:з」∠)_ 右键你的IDE快捷 ...
- linux性能分析工具Procs
- smbtar - 直接备份SMB/CIFS共享资源到UNIX磁带设备的shell脚本
总览 smbtar -s server [-p password] [-x service] [-X] [-d directory] [-u user] [-t tape] [-b blocksize ...
- Android学习电子书
- python读取pcap包
import struct class FileConvert(object): ''' test python file''' def __init__(self): self.aa = 0 sel ...
- OtterCTF - Reverse - Msg Me This
原文地址:Msg Me This 题目 Category: Reverse Engineering Points: 500 Solves: 15 Description: Rick created a ...
- robotframework的if else
- 给mysql一百万条数据的表添加索引
直接alter table add index 添加索引,执行一个小时没反应,并且会导致锁表:故放弃该办法,最终解决办法如下: 一.打开mysql 命令行客户端 这里我们那可以看到导出的数据文件所存放 ...
- 左上角小猫猫直达博主GitHub \-_-/
GitHub上有博主代码工程学习笔记啥的,由于推送比较方便所以有些学习笔记就没有上传到博客园