Python3解leetcode Rotate Array
问题描述:
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)
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]
以上代码:
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)
Python3解leetcode Rotate Array的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 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 = ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- [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 ...
- [leetcode]Rotate Array
in place交换 如果是k步,那么就是把后面k个放到前面了嘛. 我们先把整个数组reverse,然后把前面的reverse回来,再把后面的reverse回来 对于AB我们要通过reverse操作得 ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- 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 ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- 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 ...
随机推荐
- 140、spring webflux 高并发的spring组件
最近公司可谓是风云变幻,年前说要拆开卖,后来说要整体卖,表示像我这种渣渣,始终逃脱不掉被卖的命运 下面进入正题 spring webflux 是spring 支持的高并发web框架,将每个http请求 ...
- PowerShell - Invoke VBA function
$xl = new-object -comobject Excel.Application $source_wb = $xl.workbooks.open($source) $xl.visible=$ ...
- 使用pycharm调试远程服务器上的程序
一.PyCharm实现远程调试代码 1. 和远程服务器建立连接,tools àDeploymentàConfiguration 2. 点击“+”号,按照远程服务器信息配置信息 点击“Advanced ...
- SpringMVC请求处理流程源码
我们首先引用<Spring in Action>上的一张图来了解Spring MVC 的核心组件和大致处理流程: 从上图中看到①.DispatcherServlet 是SpringMVC ...
- 命令行打包war包
输入jar -cvf 包名.war 目录/*
- 难倒你了吧!ArrayList 为啥要实现 RandomAccess 接口?
作者:蔡先森_caiyq https://www.jianshu.com/p/3e2a9e4c9e01 在我们的开发中,List接口是最常见不过,而且我们几乎每天都在用ArrayList或者Linke ...
- 17、NumPy——副本和视图
副本是一个数据的完整的拷贝,如果我们对副本进行修改,它不会影响到原始数据,物理内存不在同一位置. 视图是数据的一个别称或引用,通过该别称或引用亦便可访问.操作原有数据,但原有数据不会产生拷贝.如果我们 ...
- THUPC/CTS/APIO2019划水记
THUPC:划水的咸鱼 CTS:打铁 APIO:压线cu 终于又回归了文化课. 落下10天的课程,OI又得停一停了 这次划水,又见识了许多的神仙,再一次被吊打 5.11~5.20,有太多的事情需要回忆 ...
- MySQL的数据类型:文本、数字、日期/时间
在MySQL中,有三种主要的类型:文本.数字和日期/时间类型. 文本类型(text):数据类型 描述 CHAR(size) 保存固定长度 ...
- 微软Visual Studio Code基本特征
Visual Studio Code它的核心功能还是作为一个代码编辑器.和其他的代码编辑器一样,VScode采取通用的UI和布局,浏览器在左边,显示所有的文件和文件夹,右边你打开的文件的编辑页面. 文 ...