问题描述:

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

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

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?

思路:

解题思路比较多,最关键的想出尽可能多的解题方法

代码

class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
while k >= len(nums):
k -= len(nums)
if k == 0:
return
num1 = list([int])
num1[:] = nums[:]
nums[0:k] = num1[-k:]#利用num的后k个数字,替换nums的前k个数字
nums[k:] = num1[0:len(num1)-k]
nums[:] = nums[0:len(num1)]

以上代码,时间复杂度为O(1)

Runtime: 48 ms, faster than 94.32% of Python3 online submissions forRotate Array.
Memory Usage: 13.7 MB, less than 5.23% of Python3 online submissions for Rotate Array.
 
 
将代码优化:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k]

以上代码:

Runtime: 48 ms, faster than 94.32% of Python3 online submissions forRotate Array.
Memory Usage: 13.5 MB, less than 32.26% of Python3 online submissions for Rotate Array.
 
以上两组代码相比,运算时间上没有太大的变化;第二个代码少用一个数组的空间,导致空间占用会比较少一些
 
 

class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
while k >= len(nums):
k -= len(nums)
if k == 0:
return
for i in range(k):
nums.insert(0,nums[-1])
nums.pop()

以上代码,时间复杂度O(n)

Runtime: 124 ms, faster than 16.90% of Python3 online submissions forRotate Array.
Memory Usage: 13.4 MB, less than 58.82% of Python3 online submissions for Rotate Array.

Python3解leetcode Rotate Array的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

  3. LeetCode Rotate Array 翻转数组

    题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...

  4. [LeetCode] Rotate Array 旋转数组

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

  5. [leetcode]Rotate Array

    in place交换 如果是k步,那么就是把后面k个放到前面了嘛. 我们先把整个数组reverse,然后把前面的reverse回来,再把后面的reverse回来 对于AB我们要通过reverse操作得 ...

  6. Python3解leetcode Single Number

    问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...

  7. Python3解leetcode Best Time to Buy and Sell Stock II

    问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  8. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

  9. Python3解leetcode Kth Largest Element in a Stream

    问题描述: Design a class to find the kth largest element in a stream. Note that it is the kth largest el ...

随机推荐

  1. Linux运维工程师需掌握的技能

    笔者是运维工程师,对Linux方面有点心得,现在说一下需要掌握哪方面的工具吧.说到工具,在行外可以说是技能,在行内我们一般称之为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没有 ...

  2. Vagrant 入门 - 清理(teardown)

    原文地址 我们现在有一个功能齐全的虚拟机,可以用于基本 Web 开发.但如果现在需要更换设备,或者在另一个项目上工作,如何清理我们的开发环境? 借助 Vagrant,可以暂停(suspend),停止( ...

  3. vue-安装及新建一个项目

    1.首先我们需要安装node.js,下载地址是:https://nodejs.org/en/ 之后是node.js的正常安装步骤: 接着打开window+R输入cmd回车进入命令行模块 2.确认nod ...

  4. org.w3c.dom。 XML解析 练习

    HTML文档 1 import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; ...

  5. 标准标签库JSTL(JSP Standard Tag Library)

    1, 核心标签(最常用, 最重要的) 表达式控制标签 out 输出常量 value---直接赋值 输出变量 default---默认值 escapeXml---控制转义字符(默认为true, 如果需要 ...

  6. 通过Samba练习metasploit的使用

    文章学习利用metasploit通过Samba3.X获取metasploitable2的shell,从而去熟悉metasploit的使用. 环境: kali IP:192.168.137.133 me ...

  7. POJ-1287.Network(Kruskal + Prim + Prim堆优化)

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19674   Accepted: 10061 Desc ...

  8. python学习第五天--函数进阶

    局部变量与全局变量下面代码中,old_price,rite为全局变量,final_price为局部变量 globals() 声明全局变量,在函数内可修改函数外的变量 内嵌函数:函数当中嵌套函数 闭包: ...

  9. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  10. docker镜像源设置

    由于docker默认镜像源为国外官方源,下载速度较慢.设置国内镜像源可加速 修改文件 /etc/docker/daemon.json vi /etc/docker/daemon.json 添加以下内容 ...