首页 > 其他 > 详细

Rotate Array

时间:2017-09-21 23:01:19      阅读:178      评论:0      收藏:0      [点我收藏+]

    这道题为简单题

  题目:

    技术分享

 

  思路:

    我的思路:利用列表的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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!