Swap Two Integers in Array(lintcode 484)

Description

Given an array and two indexes, swap the integers on the two indices.

Example

Given [1,2,3,4] and index1 = 2, index2 = 3. The array will change to [1,2,4,3] after swapping. You don't need return anything, just swap the integers in-place.

Interface

public class Solution {
    /**
     * @param A an integer array
     * @param index1 the first index
     * @param index2 the second index
     * @return void
     */
    public void swapIntegers(int[] A, int index1, int index2) {
        // Write your code here
    }
}

Solution

public class Solution {
    /**
     * @param A an integer array
     * @param index1 the first index
     * @param index2 the second index
     * @return void
     */
    public void swapIntegers(int[] A, int index1, int index2) {
        // Write your code here
        int temp = A[index1];
        A[index1] = A[index2];
        A[index2] = temp;
    }
}

results matching ""

    No results matching ""