问题描述:

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. yii2框架的安装&配置启动

    top:环境MacBook 1.通过composer 安装yii2 [yii2需要php的PDO和pdo_mysql扩展,需要确认已安装] a. 首先需要配置composer:我使用的是阿里云的镜像: ...

  2. EZOJ #386 最小生成树

    分析 先建出最小生成树 之后每次倍增找环即可 代码 #include<bits/stdc++.h> using namespace std; #define int long long s ...

  3. 动态规划-递推-HDU2048

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2048 全错=全不匹配 设当前全错的个数是dp[n] 那么前(n-1)个全错的话,第n个数就可以从前(n- ...

  4. SEC3 - MySQL常见命令

    1.查看当前所有的数据库 show databases; 2. 打开指定的库名 use 库名称: 3.查看当前库中所有的表 show tables; 4. 查看其他库的所有表 show tables ...

  5. SpringMvc和Mybatis整合需要配置的xml

    applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...

  6. java_第一年_JavaWeb(5)

    HttpServletRequest对象 通过HttpServletRequest对象可获取客户端在访问服务器时,请求的所有信息 获取客户机的信息 getRequestURL:返回客户端发出请求时的完 ...

  7. 关于php中trim、ltrim和rtrim

    以ltrim为例 先看手册说明先 定义和用法 ltrim() 函数从字符串左侧删除空格或其他预定义字符. 语法 ltrim(string,charlist) 参数 描述 string 必需.规定要转换 ...

  8. MIT 6.824学习笔记2 RPC/Thread

    本节内容:Lect 2   RPC and Threads 线程:Threads allow one program to (logically) execute many things at onc ...

  9. navigate连接不上Centos7+mariadb的问题

    链接数据库时忽然遇到一个问题.Mac Navicat链接时报错Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (61). PS. win版Navicat ...

  10. 2018-8-10-win10-uwp-MetroLog-入门

    title author date CreateTime categories win10 uwp MetroLog 入门 lindexi 2018-08-10 19:16:53 +0800 2018 ...