【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 ...
随机推荐
- 6/7 Sprint2 看板和燃尽图
- Qt之QRadioButton
简述 QRadioButton部件提供了一个带有文本标签的单选框(单选按钮). QRadioButton是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮.单选框通常呈现 ...
- JavaWeb基础:HTTP协议和基于Restful的架构
HTTP介绍 HTTP协议是互联网上应用最广泛的协议,它是一种无状态的数据传输协议,规定了数据请求方和数据响应方的数据传输方式:使用HTTP协议可以跨平台,跨语言的进行数据传输和展示. 目前的Web应 ...
- hdu---(Tell me the area)(几何/三角形面积以及圆面积的一些知识)
Tell me the area Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- JSON Viewer
http://jsonviewer.codeplex.com/ jsoneditor https://github.com/josdejong/jsoneditor
- JavaScript第一部分
一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...
- HDU 1171(01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- java面试---summay
1:合适的建立索引,数据量比较大的时候,如果频繁的进行修改插入则不建议建立索引! 2:什么时候适合建索引,在什么字段上面建立索引? (被当做查询条件的) 3:什么叫做编译错误,什么叫做运行时异常 能被 ...
- CPU厂商
1·Intel公司 Intel是生产CPU的老大哥,它占有大约80%的市场份额,Intel生产的CPU就成了事实上的x86CPU技术规范和标准.最新的酷睿2成为CPU的首选. 2·AMD公司 除了In ...
- Cassandra——类似levelDB的基于p2p架构的分布式NOSQL数据库
C: Consistency 一致性 • A: Availability 可用性(指的是快速获取数据) • P: Tolerance of network Partition 分区容忍性(分布式) 1 ...