作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/rotate-array/description/

题目描述

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

题目大意

把一个数组向右移动k步,原地操作。

解题方法

切片

题目要求旋转,而且是从右边数k个值放到前面去进行旋转。需要注意的两点:

  1. k可能很大,也就是会旋转多次
  2. 原地翻转

下面的做法是先去除了多次旋转的情况,然后通过遍历在原地翻转

class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
while k > len(nums):
k -= len(nums)
changed = nums[-k:] + nums[:-k]
for i in range(len(changed)):
nums[i] = changed[i]

更优雅的方法,使用k对len进行求余,另外使用nums[:]即可原地翻转。

class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
k %= len(nums)
nums[:] = nums[-k:] + nums[:-k]

递归

先把整个的翻转,再把前k个翻转,再把后面的翻转。

class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
N = len(nums)
k %= N
self.reverse(nums, 0, N - 1);
self.reverse(nums, 0, k - 1);
self.reverse(nums, k, N - 1); def reverse(self, nums, start, end):
while start <= end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1

日期

2018 年 2 月 5 日
2018 年 11 月 29 日 —— 时不我待

【LeetCode】189. Rotate Array 解题报告(Python)的更多相关文章

  1. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

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

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

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

  7. LeetCode 896 Monotonic Array 解题报告

    题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...

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

  9. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. No.2 R语言在生物信息中的应用—模式匹配

    目的: 1. 计算自定义模序在所有蛋白质的匹配位点和次数 2. 输出超过阈值的蛋白质序列到Hit_sequences.fasta 3. Hit_sequences.fasta中序列用小写字母,匹配用大 ...

  2. Python—python2.7.5升级到2.7.14或者直接升级到3.6.4

    python2.7.5升级到2.7.14 1.安装升级GCC yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2 ...

  3. Excel-计算年龄、工龄 datedif()

    函数名称:DATEDIF 主要功能:计算返回两个日期参数的差值. 使用格式:=DATEDIF(date1,date2,"y").=DATEDIF(date1,date2," ...

  4. mongodb-to-mongodb

    python3用于mongodb数据库之间倒数据,特别是分片和非分片之间. 本项目是一个集合一个集合的倒. 参考了logstash,对于只增不减而且不修改的数据的可以一直同步,阻塞同步,断点同步.改进 ...

  5. 日常Java 2021/10/24

    Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...

  6. 转Android service 启动篇之 startForegroundService

    本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...

  7. Linux学习 - 文件包处理命令

    一.搜索文件find find  [搜索范围]  [匹配条件] (1) -name(名字查找) <1>  find  /etc  -name  init 查找/etc下以 "in ...

  8. Unity——WegGL打包问题

    Rendering设置 Gamma和Linear颜色空间,两者有色差,Gamma有个2.25左右的修正值: WebGL2.0可用的情况,只支持Deferred Render延迟渲染,且只支持Linea ...

  9. selenium: where to get ChromeDriver?

    address: http://npm.taobao.org/mirrors/chromedriver

  10. 30个类手写Spring核心原理之MVC映射功能(4)

    本文节选自<Spring 5核心原理> 接下来我们来完成MVC模块的功能,应该不需要再做说明.Spring MVC的入口就是从DispatcherServlet开始的,而前面的章节中已完成 ...