面试题(9)之 leetcode-189
题目描述

解法一:
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead. - 不要返回任何内容,而是在适当的位置修改nums。
*/
var rotate = function(nums, k) {
let length = nums.length
let tailArr = nums.slice(length - k)
nums.unshift(...tailArr)
nums.splice(length, k)
};
解法二:
var rotate = function(nums, k) {
while (k--) {
// 每次将nums最后的元素切换到开头
nums.splice(0, 0, nums.pop())
}
};
解法三:
var rotate = function(nums, k) {
while (k--) {
nums.unshift(nums.pop())
}
console.log(nums)
};
解法四:
思路: 截取、连接,不使用 concat 方法保证空间O(1)
var rotate = function(nums, k) {
let a = nums.splice(nums.length-k);
nums.splice(0,0,...a)
console.log(nums)
};
面试题(9)之 leetcode-189的更多相关文章
- 前端与算法 leetcode 189. 旋转数组
目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...
- [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. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- leetcode 189
189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and ...
- 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 ...
- Java实现 LeetCode 189 旋转数组
189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] ...
- Leetcode 189 Rotate Array stl
题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...
- Java for 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 ...
- Java [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 ...
- C#解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 ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:文本框(Textarea)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Py2与Py3的区别
总结Py2 与Py3 的区别 1 编码区别 在Python2中有两种字符串类型str和Unicode. 默认ASCII python2 str类型,相当于python3中的bytes类型 python ...
- PaperReading20200226
CanChen ggchen@mail.ustc.edu.cn To share or not share Motivation: With the publiaction of NAS101, ...
- jQuery Validation Engine(二) checkHello data-errormessage
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- python笔记12
day12 今日内容 函数中高级(闭包/高阶函数) 内置函数 内置模块(.py文件) 内容回顾 函数基础概念 函数基本结构 def func(arg): return arg; v1 = func(1 ...
- IOS 3种内省方法
IOS提供了3种内省方法 isKindOfClass 检查当前实例是否为某类及其子类 UIView *b = [UIView new]; //... id a = b; if ([a isMember ...
- C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持. using System.ComponentModel; using System.Drawing; using System. ...
- BugFix系列---开篇介绍
这个系列的文章,主要目的在于积累总结实际开发中遇到的错误,记录下来自己的解决思路,用来提升自己. 不出意外,应该会持续不断的记录更新,在整个开发openstack的过程中,抓住机会吸取开源界大牛的 ...
- Socket传输大文件(发送与接收)
下载 Client using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- javascript 在页面不刷新的情况下 其中的变量时不会被初始化的
因此可以根据这个原理做出一些效果,例如 <html> <head> <meta charset="utf-8" /> ...