Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
Implement the Solution class:
Solution(int[] nums) Initializes the object with the integer array nums.
int[] reset() Resets the array to its original configuration and returns it.
int[] shuffle() Returns a random shuffling of the array.
classSolution { int[] data; int[] shuffle; Random r; publicSolution(int[] nums) { data = nums; shuffle = newint[nums.length]; r = newRandom(); for(inti=0; i < data.length; i++){ shuffle[i] = nums[i]; } } publicint[] reset() { return data; } publicint[] shuffle() { for(inti= data.length-1 ; i >= 0; i--){ swap(i, r.nextInt(i+1)); } return shuffle; } privatevoidswap(int i, int j){ inttemp= shuffle[i]; shuffle[i] = shuffle[j]; shuffle[j] = temp; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
classSolution { int[] data; Random r; publicSolution(int[] nums) { data = nums; r = newRandom(); } publicint[] reset() { return data; } publicint[] shuffle() { ArrayList<Integer> bin = newArrayList<>(); for(inti=0; i < data.length; i++){ bin.add(data[i]); } int[] ret = newint[data.length]; for(inti=0 ; i < data.length; i++){ intrandom= r.nextInt(bin.size()); ret[i] = bin.get(random); bin.remove(random); } return ret; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ publicclassCodec {
// Encodes a tree to a single string. public String serialize(TreeNode root) { StringBuilderdata=newStringBuilder(); Queue<TreeNode> q = newLinkedList<>(); q.add(root); while(!q.isEmpty()){ TreeNodecurr= q.poll(); if(curr == null) data.append("n,"); else { data.append(curr.val + ","); q.add(curr.left); q.add(curr.right); } } return data.toString(); }
// Your Codec object will be instantiated and called as such: // Codec ser = new Codec(); // Codec deser = new Codec(); // TreeNode ans = deser.deserialize(ser.serialize(root));