面试题(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 ...
随机推荐
- nginx反向代理(2)
目录 nginx缓存 基本概念 常用模块 proxy_cache 超时相关 常见架构 ========================================================= ...
- Python字符串(二)
四.类型转换 1. 基本语法: 类型名(数据) --- 将指定数据转换成指定类型 说明:类型名 -任何python支持的,或者自定的类型都可以数据 -需要转换的对象,类型不同要求可能不一样 2. 转换 ...
- 多线程分析之Semaphore
Semaphore分析由来 网上看了许多讲解Semaphore的,用Semaphore来实现顺序打印字母,但是可能大家都没有清楚具体的原因,所以来给大家分析下为什么可以使用Semaphore来实现顺序 ...
- 启动kafka报错
启动kafka时 报错: kafka-console-consumer.sh --from-beginning --zookeeper node01:8121,node02:8121,node03:8 ...
- computed、methods、watch
computed:计算属性将被混入到 Vue 实例中.所有 getter 和 setter 的 this 上下文自动地绑定为 Vue 实例. methods:methods 将被混入到 Vue 实例中 ...
- ch5 创建类似按钮的链接
锚是行内元素,所以只有在单击链接的内容时它们才会激活,如果可以实现为按钮的效果,就可以有更大的可单击区域,实现方法为:display设置为block,修改width.height和其他属性,代码如下: ...
- 图的数据结构的实现与遍历(DFS,BFS)
//图的存储结构:const int MAXSIZE = 10;//邻接矩阵template<class T>class MGraph {public: MGraph(T a[], ...
- Hive事务原理和Datax同步事务表问题解决
一.事务的概述 1.定义 事务就是一组单元化操作,这些操作要么都执行,要么都不执行,是一个不可分割的工作单位. 2.特点 事务(transaction)具有的四个要素:原子性(Atomicity).一 ...
- api文档方法参数
in型参数,带信息进去用: out型参数,方法执行结束,带着信息出来 如: CreateProcessW( _In_opt_ LPCWSTR lpApplicationName, _I ...
- SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
[oracle@jtwy02 ~]$ sqlplus '/as sysdba' SQL*Plus: Release 11.2.0.4.0 Production on Sat Oct 13 14:14: ...