【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 ...
随机推荐
- 默认选择radio的第一个
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js中String.prototype.format類似于.net中的string.formitz效果
String.prototype.format = function(args) { if (arguments.length>0) { var result = this; if (argum ...
- A New Tetris Game
时间限制(普通/Java):1000MS/10000MS 运行内存限制:65536KByte 总提交: 40 测试通过: 12 描述 曾经,Lele和他姐姐最喜欢,玩得最 ...
- EasyUI tab常用
获取选中的tab的title var tab = $('#tab_Employee').tabs('getSelected'); var t=tab.panel('options').title; t ...
- js刷新页面和跳转
javascript返回上一页: 1.返回上一页 history.go(-1); 返回上两个页面 history.go(-2); <a href="javascript:history ...
- 闭包 (循环事件获取不到i) 和 各种解决循环获取不到i的解决方法
for(var i in fav){ (function(){ var p=i; var obj=$S.getId(fav[i]); ...
- 文本信息“welcome to java programming!”
import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...
- 两天以来对plsqldev操作的记忆
frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...
- UVa 10561 - Treblecross
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 向MySql中插入中文时出现乱码
这个因为字符集编码问题.在连接字符串中加上CharSet=gbk