题目描述

解法一:

/**
 * @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的更多相关文章

  1. 前端与算法 leetcode 189. 旋转数组

    目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...

  2. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  3. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  4. leetcode 189

    189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and ...

  5. 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  ...

  6. Java实现 LeetCode 189 旋转数组

    189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] ...

  7. 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,.. ...

  8. 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 ...

  9. 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 ...

  10. 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  ...

随机推荐

  1. scrapy 开发流程

    一.Spider 开发流程 实现一个 Spider 子的过程就像是完成一系列的填空题,Scrapy 框架提出以下问题让用户在Spider 子类中作答: 1.爬虫从哪个或者那些页面开始爬取? 2.对于一 ...

  2. UniCode 速查表

    unicode速查表 0000–0FFF 1000–1FFF 2000–2FFF 3000–3FFF 4000–4FFF 5000–5FFF 6000–6FFF 7000–7FFF 8000–8FFF ...

  3. Spring和SpringMVC的直接

    1.Spring的常用注解 2.SpringMVC的常用注解

  4. 今日份学习: springboot 用到的注解

    笔记 上回用到的所有注解 @Around @Aspect @Autowired @Bean @Configuration @RequestMapping @ResponseBody @RestCont ...

  5. VUE 父子组件之间通信传值 props和 $emit

    1.父组件传值给子组件 $props,子组件传值给父组件 $emit 父组件          <div id="app" >               <tr ...

  6. Day2-E-Catch That Cow-POJ3278

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  7. sklearn中实现多分类任务(OVR和OVO)

    sklearn中实现多分类任务(OVR和OVO) 1.OVR和OVO是针对一些二分类算法(比如典型的逻辑回归算法)来实现多分类任务的两种最为常用的方式,sklearn中专门有其调用的函数,其调用过程如 ...

  8. 吴裕雄--天生自然JAVA数据库编程:JDBC2.0操作

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  9. arm linux 移植 MQTT (paho、mosquitto)

    前言 我们在这里做2件事情: 1)编译 paho.mqtt.mosquitto 2个开源项目的c版本库(mosquitto库没有用上) 2)编译好 依赖 paho.mqtt的库编写例程 + mosqu ...

  10. hadoop-mapreduce的官方示例的测试执行方法

    1.根据给出的精度参数计算 pi : hadoop jar /export/servers/hadoop-2.6.0-cdh5.14.0/share/hadoop/mapreduce/hadoop-m ...