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. easyui的基本用法

    之前有用过extjs,最近发现easyui和fineui和extjs比较类似,并且稍微简单一点,所以考虑使用. 以下是项目中的具体简单应用 function callback2d(data) {//d ...

  2. JavaScript实现数组转置

    //数组转置  var  arr=[[2,5,6,8],[8,5,6,9],[9,6,2,1]] ;   var arrNew=[];//定义一个新数组   for(var i=0;i<arr[ ...

  3. 玩转linux文件(重点)

    一.几个主要的操作 mkdir—创建目录 cp—复制文件和目录 mv——移动/重命名文件和目录 rm——删除文件和目录 ln——创建硬链接和软链接 二.几个考点: 通配符 硬链接和软链接(符号链接) ...

  4. Help Me Escape (ZOJ 3640)

    J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB ...

  5. extjs 控件属性

    1.textfield labelSeparator :''   这个属性当fieldLabel有值得时候,默认会加上一个分号,这个属性就是控制那个的... 2.numberfield decimal ...

  6. 在 Visual C# 项目中调用 VBA 中的代码

    https://msdn.microsoft.com/zh-cn/library/Bb608613.aspx http://www.cnblogs.com/yangbin1005/archive/20 ...

  7. [转]Web基础架构:负载均衡和LVS

    以下内容转载自:http://www.importnew.com/11229.html 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服 ...

  8. 原创:整理编辑jQuery全部思维导图【附下载地址】

    主图 全部图已经打包:下载地址 2. 3. 4. 5. 6. 附上一点简单说明 Dom对象和jquer对象之间的转化 如何将一个jquery对象转换为DOM对象? test是一个span元素 var ...

  9. redhat enterprixe 5.0 samba 服务器 rpm 安装及配置

    Samba是著名的开源软件项目,在Linux/UNIX系统中实现了SMB/CIFS网络协议,因此使得跨平台的文件共享变得容易.在部署Windows.Linux/UNIX混合平台的企业环境时,使用Sam ...

  10. 在Ios里UIWebView参入js

    //修改图片大小适应webView宽高度            [webView stringByEvaluatingJavaScriptFromString:       @"var sc ...