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)

  1. class Solution:
  2. # @param nums, a list of integer
  3. # @param k, num of steps
  4. # @return nothing, please modify the nums list in-place.
  5. def rotate(self, nums, k):
  6. n = len(nums)
  7. k = k % n
  8. nums[:] = nums[n-k:] + nums[:n-k]

要注意一个问题:

A little important thing to be cautious:

  1. nums[:] = nums[n-k:] + nums[:n-k]

can't be written as:

  1. 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.

因为题目要求的是:

  1. @return nothing, please modify the nums list in-place.
  2.  
  3. 类似的还有:
  1. def purify(lsst):
  2. lst = lsst[:]
  3. for num in lsst:
  4. if num % 2 == 1:
  5. lst.remove(num)
  6. return lst

这是清除 list 中的奇数,要求不要在原输入上直接修改。

注意第二行不能 写成:

  1. lst = lsst
  2.  
  3. 而应该是:
  1. lst = lsst[:]
  2.  
  3. 这样才是值相同的两个list, 否则 lst = lsst 只是一个 list 的两个引用。
  4.  
  5. 引用 的问题当属 Python 里的第一大坑。
  1.  

【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. 默认选择radio的第一个

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. js中String.prototype.format類似于.net中的string.formitz效果

    String.prototype.format = function(args) { if (arguments.length>0) { var result = this; if (argum ...

  3. A New Tetris Game

    时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte 总提交: 40            测试通过: 12 描述 曾经,Lele和他姐姐最喜欢,玩得最 ...

  4. EasyUI tab常用

    获取选中的tab的title var tab = $('#tab_Employee').tabs('getSelected'); var t=tab.panel('options').title; t ...

  5. js刷新页面和跳转

    javascript返回上一页: 1.返回上一页 history.go(-1); 返回上两个页面 history.go(-2); <a href="javascript:history ...

  6. 闭包 (循环事件获取不到i) 和 各种解决循环获取不到i的解决方法

    for(var i in fav){ (function(){                var p=i;                var obj=$S.getId(fav[i]);     ...

  7. 文本信息“welcome to java programming!”

    import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...

  8. 两天以来对plsqldev操作的记忆

    frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...

  9. UVa 10561 - Treblecross

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. 向MySql中插入中文时出现乱码

    这个因为字符集编码问题.在连接字符串中加上CharSet=gbk