【LeetCode】Rotate Array
Rotate Array
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
AC代码:(Python)
class Solution:
# @param nums, a list of integer
# @param k, num of steps
# @return nothing, please modify the nums list in-place.
def rotate(self, nums, k):
n = len(nums)
k = k % n
nums[:] = nums[n-k:] + nums[:n-k]
要注意一个问题:
A little important thing to be cautious:
nums[:] = nums[n-k:] + nums[:n-k]
can't be written as:
nums = nums[n-k:] + nums[:n-k]
on the OJ.
The previous one can truly change the value of old nums, but the following one just changes its reference to a new nums not the value of old nums.
因为题目要求的是:
@return nothing, please modify the nums list in-place. 类似的还有:
def purify(lsst):
lst = lsst[:]
for num in lsst:
if num % 2 == 1:
lst.remove(num)
return lst
这是清除 list 中的奇数,要求不要在原输入上直接修改。
注意第二行不能 写成:
lst = lsst 而应该是:
lst = lsst[:] 这样才是值相同的两个list, 否则 lst = lsst 只是一个 list 的两个引用。 值 和 引用 的问题当属 Python 里的第一大坑。
【LeetCode】Rotate Array的更多相关文章
- 【LeetCode】 Rotate List 循环链表
题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【题解】【矩阵】【回溯】【Leetcode】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
随机推荐
- Github/Eclipse管理Maven项目
Eclipse和Git插件 (To-do: 直接从workspace导入也可以,弄明白这个repo管理的本质,查看sprigmvc是如何导入的) 最新版本的Eclipse都直接集成了Git插件 Ecl ...
- Git删除远程分支
查看 git branch -a 删除远程分支 git branch -r -d origin/branch-name //只是删除的本地对该远程分支的track git push origin : ...
- 用HTML5实现手机摇一摇的功能(转)
在百度开发者大会上我介绍过HTML5另外一个重要特性就是DeviceOrientation,它将底层的方向传感器和运动传感器进行了高级封装,提供了DOM事件的支持.这个特性包括两种事件: 1.devi ...
- java json 的生成和解析 --json-lib
类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; im ...
- [工作技能]SVN
有的时候SVN上传txt文本文件,会报是bin文件的错误,解决方式是在.subversion文件夹下的config文件中加这么一句 *.txt = svn:mime-type=text/plain;s ...
- cl.exe
http://blog.csdn.net/happyanger6/article/details/7589016
- jar转dll
IKVM http://www.cnblogs.com/luckeryin/archive/2012/03/28/2421274.html
- Excel VBA记录
-----------快捷键---------- 函数等提示(默认):ctrl+j 注释:上单引号' 设置单元格为空可以用:empty/null -----------基础语法--------- 基本 ...
- Storm(1) - Setting Up Development Environment
Setting up your development environment 1. download j2se 6 SDK from http://www.oracle.com/technetwor ...
- SSL证书请求文件(CSR)生成指南 - Tomcat
SSL证书请求文件(CSR)生成指南 - Tomcat http://www.zhenssl.com/support/CSRgen/tomcat_CSR.htm 重要注意事项 An Importa ...