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{
String str;
String[] strs;int index =0;// Encodes a tree to a single string.public String serialize(TreeNode root){
str ="";
sh(root);return str;}voidsh(TreeNode node){if(str.length()>0) str +=",";if(node==null){
str +="#";}else{
str += node.val;
sh(node.left);
sh(node.right);}}// Decodes your encoded data to tree.public TreeNode deserialize(String data){
strs = data.split(",");
index =0;returndh();}
TreeNode dh(){
TreeNode node;
String s = strs[index++];if(s.equals("#")){
node =null;}else{
node =new TreeNode(Integer.parseInt(s));
node.left= dh();
node.right= dh();}return node;}}// 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));
Question:
Two players take numbers from a set of numbers nums in turn. A player can only take one number from nums each time. Once a number is taken, it is removed from nums. The sum of all the removed number is denoted by sum. A player wins if sum>target after the player took one number.
Given nums and target, determine whether the first player can win assuming all the numbers is nums and target are positive.
Eg1., if nums = {2,3} and target = 4, the first player loses no matter which number it takes first.
Eg2., if nums = {1,2,3} and target = 4, the first player can win by remove 1 at the 1st step. Then the second can only take 2 or 3. In either case the first player wins by taking the last number left.
Ref
[1] http://www.mitbbs.com/article_t/JobHunting/33010083.html
Question: Given a sequence of numbers, find the length of the longest palindrome subsequence. Eg., if nums = {1,2,2,0,1}, it should return 4 because the longest palindrome subsequence is {1,2,2,1}. longestCS_rec "Time complexity of the above naive recursive approach is O(2^n) in worst case and worst case happens when all characters of X and Y mismatch i.e., length of LCS is ."[2]
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input:[1,2,3,4]Output:[24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
classSolution{public:
vector<vector<int>>permuteUnique(vector<int>& nums){
vector<vector<int>> ret;
sort(nums.begin(), nums.end());
bt(nums, ret,0, nums.size());return ret;}//nums cannot be reference so that nums[pos+1..n-1] is in increasing ordervoidbt(vector<int> nums, vector<vector<int>>&ret,int pos,int n){if(pos==n){
ret.push_back(nums);}else{for(int i=pos; i<n;++i){if(i>pos && nums[i]==nums[pos])continue;
swap(nums[pos], nums[i]);
bt(nums, ret, pos+1, n);//cannot swap back; otherwise nums[pos+1..n-1] will not be in increasing order}}}};
===========
Note It is important to keep the increasing order of the non-determined portion of the vector, ie., nums[pos+1, n-1], such that we can conveniently skip the duplicate cases by line 17.
An example for the recursion of nums. pos=0. Note that nums[1, 4] are in increasing order. 0 1 2 3 4 -- index 1 2 3 4 5 2 1 3 4 5 3 1 2 4 5 4 1 2 3 5 5 1 2 3 4
If nums is swapped back at line20. nums[1, 4] are no longer in increasing order. 0 1 2 3 4 -- index 1 2 3 4 5 2 1 3 4 5 3 2 1 4 5 4 2 3 1 5 5 2 3 4 1