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的更多相关文章

  1. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

  2. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  4. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】Rotate Image

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  6. 【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 ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【题解】【矩阵】【回溯】【Leetcode】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. 【LeetCode】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

随机推荐

  1. hdu---(3779)Railroad(记忆化搜索/dfs)

    Railroad Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. SQL Server数据库(SQL Sever语言 CRUD)

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  3. NABCD模型(猫咪记单词)

    项目需求分析与建议-NABCD模型(猫咪记单词)   N (Need 需求) 对于现在的学生,尤其是大学生来说,学习英语是一件非常重要的事.我们有四级六级托福雅思等各种各样的英语方面的考试.而学习英语 ...

  4. [python学习]结合开源库学习python

    代码碎片1 #!/usr/bin/python # please add your code here! import matplotlib matplotlib.use('Agg') import ...

  5. [转载]Windows 7笔记本创建wifi热点供手机上网教程

    用智能手机的朋友会发现这样一个问题,智能手机比普通手机上网更耗流量.这是因为智能手机应用(软件)丰富,而且大部分应用都会自动联网.为此,许多人每月包了上百M的流量套餐,但用的时候还是小心翼翼,生怕流量 ...

  6. eclipse-mysql-tomcat搭建jspk开发环境

    ...本来不想写,刚刚给女朋友又安了一次发现几乎忘了,还是记一下吧.. 1.默认安装好jdk以及eclipse或相关ide. 2.检查jdk环境变量是否配置成功:cmd下输入 java -versio ...

  7. jsp中JavaBean的用法

    UserRegisterBean.java:这是JavaBean package JavaBean; public class UserRegisterBean { private String us ...

  8. 根据图片Uri获得图片文件

    2013-12-17 1. 根据联系人图片Uri获得图片文件并将它显示在ImageView上, 代码如下: Uri uri = Uri.parse("content://com.androi ...

  9. wp8.1 C#技巧: Data和ViewModel类编写

    在Data.cs namespace PicApp { [DataContract] class DataItem : PropertyChangeNotification { public even ...

  10. opencv+ffmpeg实现avi视频的播放

    配了一天,终于成功的在ubuntu上安装了ffmpeg,实现了opencv对avi文件的读取. 在CvCapture* pCapture=cvCaptureFromAVI("video.av ...