码迷,mamicode.com
首页 > 其他好文 > 详细

Rotate Array

时间:2017-09-21 23:28:17      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:self   list   lease   col   ace   solution   ase   mod   return   

    这道题为简单题

  题目:

    技术分享

 

  思路:

    我的思路:利用列表的insert和pop方法进行操作

    大神:利用分片操作,效率明显高得多

  代码:

    我的代码:

1 class Solution(object):
2     def rotate(self, nums, k):
3         """
4         :type nums: List[int]
5         :type k: int
6         :rtype: void Do not return anything, modify nums in-place instead.
7         """
8         for i in range(k):
9             nums.insert(0, nums.pop())

    大神的:

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]

 

Rotate Array

标签:self   list   lease   col   ace   solution   ase   mod   return   

原文地址:http://www.cnblogs.com/liuxinzhi/p/7571720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!