
本文共 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
���������������������������������
������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
