面试题(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 增删改查范例(2)
增加页面add.php: <!DOCTYPE html><html lang="en"><head> <meta charset=& ...
- ecshop 资料整理
ecshop 资料整理 lib_base.php 基础函数库1.sub_str($str, $length = 0, $append = true) 截取UTF-8编码下字符串的函数 string ...
- python面向对象之练习题2
练习题 需求: 士兵 可以 花钱买一个AK47 士兵 可以 用开开火 士兵 可以 买弹夹 士兵 可以 上子弹 士兵 可以 给 枪 添加子弹 枪 需要弹夹和有子弹的情况下,借助士兵扣动扳机 才能开火 枪 ...
- 使用EA画流程图
https://www.sparxsystems.com.au/enterprise_architect_user_guide/13.0/model_domains/flow_chart.html
- 库克承认iPhone销售不佳是因定价太高,但降价能救苹果吗?
iPhone定价愈来愈高,已经是不争的事实.但iPhone价格的飙升,其实并不是"正常"的.早在乔布斯时代,iPhone的价格维持在5000元左右.虽然看起来价格略高,但也在很多人 ...
- 树莓派中实现ll命令
用管了centos的童鞋们,到了一个没有ll命令的环境里,那是多么的痛苦,在baidu后,将实现方法记录如下 方法一: echo "alias ll='ls -l'" >&g ...
- 使用redis集群中遇到的错误
一. 上述错误的原因: 1.在redis服务器上关闭防火墙 2.可能是host写错了 上述错误的原因: 配置文件中jedisClient代表的是单机版的redis,但是在类中转化的时候转化的是集群版
- 「CF10D」LCIS
传送门 Luogu 解题思路 首先考虑怎么求方案,这样才可能会输出方案. 考虑 \(\text{DP}\). 设 \(f[i][j]\) 表示在 \(a\) 序列中选择一个 \([1...i]\) 的 ...
- Python入门知识总结【新手必学】
Python 基础学习输入和输出list 和 tuple条件判断循环dict 和 set感觉python这门语言用途较广,先熟悉下其语法.PS:另外很多人在学习Python的过程中,往往因为没有好的教 ...
- hyper-V pcie直通
横河VI702失败. 显卡没有提示失败,但分配给的那个虚拟机中 检测不到独占的显卡,不知道那里出错了. 参考教程22#:[网络] 既然最新hyper V也支持pcie直通.是不是可以扔掉esxi了?