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 ...
随机推荐
- CentOS 7.0 开端口
>>> CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下1.直接关闭防火墙systemctl stop firewalld.serv ...
- 使用定时器settimeout、setInterval执行能传递参数的函数
无论是window.setTimeout还是window.setInterval,在使用函数名作为调用句柄时都不能带参数,而在许多场合必须要带参数,这就需要想方法解决.经网上查询后整理如下: 例如对于 ...
- MongoDB ODM
安装 pip3 install mongoengine 连接MongoDB 方法一:简写 connect('students) 方法二:指定端口和地址 connect('students',host= ...
- jQuery提交表单的几种方式
方式一: $.post('../Ajax/GoodsAjax.ashx?cmd=getGsList', function (result) { var result = eval('(' + re ...
- canvas添加事件
https://blog.csdn.net/xundh/article/details/78722744
- Linux服务正常启动,Linux服务器能访问,但是外部机器不能访问
公司用到了jenkins,就在自己虚拟机里面部署了一个jenkins.部署成功之后,在Linux虚拟机里面能正常访问,但是外部真实机却不能访问.当时的第一反应就是觉得应该是权限问题,猜测会不会是jen ...
- 【五一qbxt】day6 OI中的stl
from:why 很多很多part…… 1.pair: 相当于把两个变量放在一起: #include<utility> using namespace std; pair<TypeN ...
- Yii中CreateUrl的使用总结
在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...
- 完美的Linux之【navi】使用笔记
今天要说的是才上线才两天,就已经获得超过1000星.开发者是一位来自巴西的小哥Denis Isidoro. 开发的工具navi Linux用户的日常困惑 新命令 用完就忘 ? 一时想不起来命令的单词怎 ...
- NGUI的Lebal需注意问题
1,为什么调节字体大小时,字体大小没变化,我们需要调节两个地方,如下图框柱显示 调节font size和size才可以同时控制字体的大小 2,label有时是不支持输入中文,但是支持复制进去,则这时我 ...