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. cf--------(div1)1A. Theatre Square

    A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  2. 164. Maximum Gap *HARD* -- 无序数组找出排序后连续元素的最大间隔

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  3. Andriod使用webview控件往APP里内嵌网页

    转自博文:http://www.cnblogs.com/JuneZhang/p/4148542.html 1.布局文件片段:res-layout <WebView android:id=&quo ...

  4. HTML5标签学习之~~~

    <article> 标签 article 字面意思为“文章”.在web页面中表现为独立的内容,如一篇新闻,一篇评论,一段名言,一段联系方式.这其中包括两方面,一为整个页面的主旨内容,另外就 ...

  5. SVN删除同名文件夹

    解     释一下:     SVN  出现这个错误的原因是我删除了一个文件夹后又创建了一个同名文件夹.  在  svn   server  端,好像是不能区分这两个文件夹,所以出现了错误.     ...

  6. java入门第二步之helloworld【转】

    前一篇博客已经介绍了jdk的安装:接下来我们就乘热打铁,完成第一个程序:helloworld(每学一样程序的新东西都是从实现helloworld开始的) 1.不是用开发工具IDE,只是使用记事本来实现 ...

  7. spring+springmvc+mybatis整合

    1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  8. ASP.NET访问Access的连接字符串配置

    由于Access是文件数据库,所以在ASP.NET需要能映射访问到Access文件: 方式一:appSettings中设置连接字符串 web.config的配置 <appSettings> ...

  9. bzoj 2243: [SDOI2011]染色

    #include<cstdio> #include<iostream> #define M 1000006 #define N 1000006 using namespace ...

  10. 登陆中session的处理

    在学校中的登陆注册使用的普通session存储信息,然后就是根据session中获取user是否拥有来判断是否登陆. 在一次面试中别人问到了我你们项目的登陆session是怎么一个情况,我这样答的话那 ...