[LeetCode] Inplace random shuffling an array

public void shuffle(int[] A) {
    if (A == null || A.length == 0) return;
    for (int i = 0; i < A.length-1; ++i) {
        int random = Math.random()*(A.length-i) + i;
        int temp = A[i];
        A[i] = A[random];
        A[random] = temp;
    }
}
posted on 2015-04-04 13:28  Seth_L  阅读(125)  评论(0编辑  收藏  举报