【leetcode】Shuffle an Array 打乱一个没有重复元素的数组
发布日期:2021-05-12 05:47:22 浏览次数:11 分类:精选文章

本文共 3805 字,大约阅读时间需要 12 分钟。

������������

������������������������������������������������������������������������������������


��������������������������������� ������������Java���Solution���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


���������

public class Solution {
// ���������������������������������������
private int[] origin;
// ������������������������������������������������
public Solution(int[] nums) {
origin = nums;
}
// ���������������������������������������������������
public int[] reset() {
return origin;
}
// ������������������������������������������
public int[] shuffle() {
Random random = new Random();
int[] nums = (int[]) origin.clone(); // ������������������������������
// ������������������������������������������������
for (int i = 0; i < origin.length; i++) {
int randomIndex = random.nextInt(origin.length - i); // ������������������������������������������
int temp = nums[i];
nums[i] = nums[randomIndex];
nums[randomIndex] = temp;
}
return nums;
}
}

������������������

���������������������������������������������������������shuffle()���������������������������������������������������

int[] nums = {1, 2, 3};
Solution solution = new Solution(nums);
// ������shuffle������������������
solution.shuffle();
// ������reset���������������������������
solution.reset();

������������������������������������������

������������������������������������������������Java���Solution������������������������������������������������������������������������������������������������������������������������������������

���������

public class Solution {
// ���������������������������������
private int[] origin;
// ���������������������������������������������
public Solution(int[] nums) {
origin = Arrays.copyOf(nums, nums.length);
}
// ���������������������������������������������������
public int[] reset() {
return (int[]) origin.clone();
}
// ������������������������������������������������������������
public int[] shuffle() {
Random random = new Random();
int[] nums = Arrays.copyOf(origin, origin.length);
for (int i = 0; i < origin.length; i++) {
int randomIndex = random.nextInt(origin.length - i);
int temp = nums[i];
nums[i] = nums[randomIndex];
nums[randomIndex] = temp;
}
return nums;
}
}

������������

������������������������������������������������������������������

int[] nums = {1, 2, 3};
Solution solution = new Solution(nums);
// ������������������
solution.shuffle();
// ���������������������
solution.reset();

������������

������������������������������������������������������

  • ���������������������������Random���������������������������������������������
  • ������������������������������shuffle()������������������O(n��)���������������������������������������������������������������������������������Fisher���Yates shuffle������
  • ������������������������������������������������������������������������������

  • ������������������

  • ���������������������������������

    • ���������������������������������������������������������������������������������������������
  • ������������������������������������������

    • ���������������������������������O(n��), ������������������������������������
  • ���������������������������

    • ���shuffle()���������������������������������Random���������������������������������

  • ������������������������������������������������������������������������������������������������������������������

    上一篇:【leetcode】最小栈
    下一篇:【leetcode】罗马数字转整数

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年05月01日 08时09分11秒