【Lintcode】767. Reverse Array

Title address:

https://www.lintcode.com/problem/reverse-array/description

Flip the array. code show as below:

public class Solution {
    /**
     * @param nums: a integer array
     * @return: nothing
     */
    public void reverseArray(int[] nums) {
        // write your code here
        int i = 0, j = nums.length - 1;
        while (i < j) {
            swap(nums, i++, j--);
        }
    }
    
    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

time complexity THE ( n ) O (n) , space THE ( 1 ) O (1)

Published 392 original articles · Liked 0 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/105469653