LintCode: Reverse Array

Reverse Array



Similar Problems:


Reverse the given array nums inplace.

Notice
Inplace means you can’t use extra space.

Example

Given nums = [1,2,5]
return [5,2,1]

Github: code.dennyzhang.com

Credits To: LintCode.com

Leave me comments, if you have better ways to solve.


## Blog link: https://code.dennyzhang.com/reverse-array
class Solution:
    """
    @param nums: a integer array
    @return: nothing
    """
    def reverseArray(self, nums):
        ## Basic Ideas: two pointer
        ## Complexity: Time O(n), Space O(1)
        l, r = 0, len(nums)-1
        while l<r:
            nums[l], nums[r] = nums[r], nums[l]
            l, r = l+1, r-1
linkedin
github
slack

Original URL: https://code.dennyzhang.com/reverse-array

Connect with Denny In LinkedIn Or Slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.