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 ...
随机推荐
- JS各种变量是true或者false列表
如果操作数是一个对象,返回true 如果操作数是一个空字符串,返回false如果操作数是一个非空字符串,返回true如果操作数是数值0,返回false如果操作数是任意非0数值(包括Infinity), ...
- docker 配置sonatype/nexus3
docker search nexusdocker pull sonatype/nexus3mkdir -p /dockermaven/nexus-datachmod -R 777 /dockerma ...
- eclipse下实现热部署,tomcat不重新reload context
1. 打开server的编辑器 2. 在modules页签内,修改auto load属性为disabled
- 设计模式 - 门面模式(Facade Pattern,也叫外观模式)
简介 场景 将系统划分为若干个子系统有利于降低系统的复杂性,但是这会增加调用者的复杂性.通过引入 Facade 可以对调用者屏蔽系统内部子系统的细节. Java 中有多个日志库,例如 log4j.lo ...
- Django+python报错与解决方法
编码信息导致修改django文件下的debug.py文件331行的编码格式 with Path(CURRENT_DIR, 'templates', 'technical_500.html').open ...
- dp或dfs(01背包问题)
链接:https://ac.nowcoder.com/acm/contest/993/C来源:牛客网题意:n头牛,给出它们的H高度,问这些牛的高度叠加起来大于等于书架高度,问叠加后的高度与书架的差值最 ...
- poj Meteor Shower
这道题是下流星,流星会下到上下左右中的位置,而且有时间的,要你求出最短到达安全位置的时间. 这道题要注意边界是可以超过300的 #include<stdio.h> #include< ...
- Go语言_并发
并发 Go 将并发结构作为核心语言的一部分提供.本节课程通过一些示例介绍并展示了它们的用法. Go 作者组编写,Go-zh 小组翻译. https://tour.go-zh.org/concurren ...
- 标签的增加、删除与复制,动态标签js不生效的解决
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- NGUI的窗体的推动和调节大小(drag object和drag resize object)
一,我们先添加一个sprite,给sprite添加一个背景图片,然后attach添加一个box Collider,但是这时我们右键attach是找不到drag object的我们需要在add comp ...