面试题(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 ...
随机推荐
- vue v-model 数据双向绑定(笔记)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL操作之DML
目录 SQL语句的分类 DML语句 SQL语句的分类 DDL(Data Definition Languages)语句:数据定义语言.这些语句定义了不同的数据段. 数据库.表.列.索引等数据库对象的定 ...
- css选择器优先级排序
浏览器默认属性 < 继承自父元素的属性 < 通配符选择器 < 标签选择器 < 类选择器 < 结构伪类选择器 < id选择器 < 行内样式 < !impo ...
- Pytorch dataset自定义【直播】2019 年县域农业大脑AI挑战赛---数据准备(二),Dataset定义
在我的torchvision库里介绍的博文(https://www.cnblogs.com/yjphhw/p/9773333.html)里说了对pytorch的dataset的定义方式. 本文相当于实 ...
- 谁说5G网络无敌?第六代Wi-Fi表示不服
导读 随着第五代移动通信技术(5G)正式商用,同属第五代的Wi-Fi技术(802.11ac)的处境就非常尴尬了,除了不存流量费用外,无论是网速.设备连接数还是网络延迟,5G都拥有秒杀802.11ac的 ...
- 5G将重新定义物联网和边缘计算
导读 比上一代蜂窝服务(4G)相比,5G提供的无线蜂窝连接性具有更高的带宽.更低的延迟和更高的设备密度. 比上一代蜂窝服务(4G)相比,5G提供的无线蜂窝连接性具有更高的带宽.更低的延迟和更高的设备密 ...
- Linux centosVMware MySQL主从介绍、准备工作、配置主、配置从、测试主从同步
一.MySQL主从介绍 MySQL主从又叫做Replication.AB复制.简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的 MySQL主从是基于binl ...
- android 根据res文件夹下(如res/raw)文件名获取其id
android 根据res文件夹下(如res/raw)文件名获取其id //测试是否能够获取其资源ID int treeId = mv.getResources().getIdentifier(fil ...
- 前端学习笔记系列一:7 在vscode中根据vue等模板生成代码
目标:希望每次新建.vue文件后,VSCODE能够根据配置,自动生成我们想要的内容. 方法:打开VSCODE编辑器,依次选择“文件 -> 首选项 -> 用户代码片段”,此时,会弹出一个搜索 ...
- Vue简介-MVVM是什么?
Vue.js - Day1 课程介绍 前5天: 都在学习Vue基本的语法和概念:打包工具 Webpack , Gulp 后5天: 以项目驱动教学: 什么是Vue.js Vue.js 是目前最火的一个前 ...