Tuesday, November 17, 2015

LinkedIn: Shuffle an array

Question:
Randomly shuffle an array. 

Code (Java):
import java.io.*;
import java.util.*;

public class Solution {
    public void shuffle(int[] A) {
        if (A == null || A.length == 0) {
            return;
        }
        
        Random generator = new Random();
        
        for (int i = 1; i < A.length; i++) {
            int randomIndex = generator.getInt(i);
            int temp = A[randomIndex];
            A[randomIndex] = A[i];
            A[i] = temp;
        }
    }
}



2 comments:

  1. 请问应该是nextInt(i + 1)吗?好像没有getInt

    ReplyDelete
  2. 应该是nextInt(i + 1), 没有getInt

    ReplyDelete