Commit 76916012 by yaozc

### Update the old uncommited changes

parent 0c359694
 ... ... @@ -444,7 +444,9 @@ public: bool helper(TreeNode* left, TreeNode* right){ if(!left && !right) return true; else if(left && right){ return left->val == right->val && helper(left->left, right->right) && helper(left->right, right->left); return left->val == right->val && helper(left->left, right->right) && helper(left->right, right->left); } return false; } ... ... @@ -3487,6 +3489,180 @@ public: Reverse Linked List /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr; ListNode* curr = head; // current is not null while(curr){ ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } }; 152. Maximum Product Subarray /* 两个 dp 数组，其中 big[i] 表示子数组 [0, i] 范围内并且一定包含 nums[i] 数字的最大子数组乘积，small[i] 表示子数组 [0, i] 范围内并且一定包含 nums[i] 数字的最小子数组乘积，初始化时 big[0] 和 small[0] 都初始化为 nums[0]， 其余都初始化为0。那么从数组的第二个数字开始遍历，那么此时的最大值和最小值只会 在这三个数字之间产生，即 big[i-1]*nums[i]，small[i-1]*nums[i]，和 nums[i]。*/ class Solution { public: int maxProduct(vector& nums) { if(nums.empty()) return 0; vector big(nums.size()); vector small(nums.size()); big[0] = nums[0]; small[0] = nums[0]; int res = nums[0]; for(int i=1; i s; // TreeNode* temp = root; // while(!s.empty() || temp){ // while(temp){ // s.push(temp); // temp = temp->left; // } // temp = s.top(); s.pop(); // //deal // cout<val<right; // } // return root; bool found = false; helper(root, p, found); return res; } void helper(TreeNode* curr, TreeNode* p, bool &found){ if(!curr) return; helper(curr->left, p, found); if(found){ res = curr; found = false; return; } if(curr == p) found = true; helper(curr->right, p, found); } }; Serialize and Deserialize Binary Tree //讲道理这题binary tree和bst一点区别也没有 //就是在重新读string进来的地方，用istringstream会很方便 //其他也没什么trick，就是遍历的问题 class Codec { private: void helper(TreeNode* root, string &res){ if(!root){ res += " #"; return; } else{ res += " "; res += to_string(root->val); helper(root->left, res); helper(root->right, res); } } TreeNode* de_helper(istringstream &is){ string temp; is >> temp; if(temp == "#"){ return NULL; } TreeNode* curr = new TreeNode(stoi(temp)); curr->left = de_helper(is); curr->right = de_helper(is); return curr; } public: // Encodes a tree to a single string. string serialize(TreeNode* root) { string res; helper(root, res); //cout<
microsoft.cpp 0 → 100644
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!