面试题(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 ...
随机推荐
- php 人员权限管理(RBAC)
一.想好这个权限是什么? 就做一个就像是vip的功能,普通用户和vip用户的功能是不一样的,先来考虑一下数据库怎么设计 肯定要有用户表.还有用户所用的角色.然后就是权限功能表:可是在这里面有关联也就 ...
- uchome后台系统配置分析
uchome后台系统配置分析 时间:2012-09-06 17:14来源:www.chinab4c.com 作者:uchome专家 uchome后台系统设置,和ecshop后台系统设置不大一样.uch ...
- Genymotion连接失败问题
adb启动问题:Invalid argument: cannot open transport registration socketpair could not read ok from ADB S ...
- QQ企业通-----客户端消息发送模块设计---知识点
Guid.NewGuid 方法 初始化 Guid 结构的新实例. Guid 结构 表示全局唯一标识符 (GUID). Encoding..::.Unicode 属性 获取使用 Little- ...
- Android将数据存储到应用的数据目录下
下面是具体代码,其中MainActivity.java的部分代码有修改,在文章后面给出 logindemo_layout.java <?xml version="1.0" e ...
- Hive事务原理和Datax同步事务表问题解决
一.事务的概述 1.定义 事务就是一组单元化操作,这些操作要么都执行,要么都不执行,是一个不可分割的工作单位. 2.特点 事务(transaction)具有的四个要素:原子性(Atomicity).一 ...
- STM32学习笔记:为什么使用外部中断要打开syscfg时钟?
AFIO时钟只是在STM32F1系列里被提及. 对于32F1系列,涉及到管脚的EXTI. REMAP.事件输出时就需要开启AFIO时钟. 比方上面提到的管脚REMAP,必须先开AFIO时钟.配置EXT ...
- WireShark 之抓包QQ协议
- vim修改缩进问题
- no-strings-attached-----攻防世界
拿到题目到虚拟机里的查看一下 把这些十进制数字转换为16进制,然后利用python deocde 注意0 87这里需要舍弃