stay Personal blog Synchronize updates in
Day 1 stack and queue (simple)
Sword finger Offer 09 Queue with two stacks
class CQueue { public: CQueue() { } stack<int>s1,s2; void appendTail(int value) { s1.push(value); } int deleteHead() { if(s2.empty()) { while(!s1.empty()) { s2.push(s1.top()); s1.pop(); } } if(s2.empty()) return -1; int tmp=s2.top(); s2.pop(); return tmp; } }; /** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
Sword finger Offer 30 Stack containing min function
Construct an auxiliary stack so that the top element of the auxiliary stack is always the minimum value of the element in the current stack
class MinStack { public: /** initialize your data structure here. */ MinStack() { } stack<int>st; stack<int>m; void push(int x) { st.push(x); if(m.empty()||m.top()>=x) m.push(x); } void pop() { if(st.top()==m.top()) m.pop(); st.pop(); } int top() { return st.top(); } int min() { return m.top(); } }; /** * Your MinStack object will be instantiated and called as such: * MinStack* obj = new MinStack(); * obj->push(x); * obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->min(); */
Day 2 linked list (simple)
Sword finger Offer 06 Print linked list from end to end
Put it in the array and reverse it
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: vector<int> reversePrint(ListNode* head) { vector<int>v; while(head) { v.push_back(head->val); head=head->next; } reverse(v.begin(),v.end()); return v; } };
Sword finger Offer 24 Reverse linked list
Let the next node of the current node point to the previous node, using a temporary pointer
/** * 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 *cnt=head,*ans=NULL; while(cnt) { ListNode *tmp=cnt->next; cnt->next=ans; ans=cnt; cnt=tmp; } return ans; } };
Sword finger Offer 35 Replication of complex linked list
await a vacancy or job opening
Day 3 string (simple)
Sword finger Offer 05 Replace spaces
Direct traversal
class Solution { public: string replaceSpace(string s) { string ans; for(auto i:s) { if(i==' ') ans+="%20"; else ans+=i; } return ans; } };
Sword finger Offer 58 - II Left rotation string
class Solution { public: string reverseLeftWords(string s, int n) { string ans=""; ans+=s.substr(n,s.size()); ans+=s.substr(0,n); return ans; } };
Day 4 search algorithm (simple)
Sword finger Offer 03 Duplicate number in array
The index and value of the construction element are in a one-to-one relationship. If the current index already has a value and is the same as the current value, it occurs multiple times
class Solution { public: int findRepeatNumber(vector<int>& nums) { int l=nums.size(); int i=0; while(i<l) { if(nums[i]==i) { i++; continue; } if(nums[nums[i]]==nums[i]) return nums[i]; swap(nums[i],nums[nums[i]]); } return -1; } };
Sword finger Offer 53 - I. find the number I in the sorted array
lower_bound and upper_ Use of bound
class Solution { public: int search(vector<int>& nums, int target) { int l_place=lower_bound(nums.begin(),nums.end(),target)-nums.begin(); int r_place=upper_bound(nums.begin(),nums.end(),target)-nums.begin(); return r_place-l_place; } };
Sword finger offer 53 - II Missing numbers from 0 to n-1
ergodic
class Solution { public: int missingNumber(vector<int>& nums) { int l=nums.size(); for(int i=0;i<l;i++) { if(nums[i]!=i) return i; } return l; } };
Dichotomy
class Solution { public: int missingNumber(vector<int>& nums) { int l=0,r=nums.size()-1; while(l<=r) { int mid=(l+r)/2; if(nums[mid]>mid) r=mid-1; else l=mid+1; } return l; } };
Day 5 search algorithm (medium)
Sword finger Offer 04 Find in 2D array
Dichotomy
Perform bisection on each line and repeat the time \ (O(n \log(m)) \)
class Solution { public: bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) { for(auto i:matrix) { int place=lower_bound(i.begin(),i.end(),target)-i.begin(); if(place!=i.size()&&i[place]==target) return true; } return false; } };
Linear search
Starting from the upper right corner, if the current element is larger than the target, go to the left; If it is smaller than target, go down. The time complexity is \ (O(n+m) \)
[font color="red"] note that the array is empty [/ font]
class Solution { public: bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) { int n=matrix.size(); if(!n) return false; int m=matrix[0].size(); int i=0,j=m-1; while(i<n&&j>=0) { if(matrix[i][j]<target) i++; else if(matrix[i][j]>target) j--; else return true; } return false; } };
Sword finger Offer 11 Minimum number of rotation array
ergodic
class Solution { public: int minArray(vector<int>& numbers) { int ans=numbers[0]; for(auto i:numbers) ans=min(ans,i); return ans; } };
Dichotomy
Note that in the case of equality, you need to traverse
class Solution { public: int minArray(vector<int>& numbers) { int l=0,r=numbers.size()-1; int ans=10000000; while(l<r) { int mid=(l+r)/2; if(numbers[mid]>numbers[r]) l=mid+1; else if(numbers[mid]<numbers[r]) r=mid; else { for(int i=l;i<=r;i++) ans=min(ans,numbers[i]); return ans; } } return numbers[l]; } };
Sword finger Offer 50 The first character that appears only once
Just save it directly with map. You can re optimize the time by removing the characters
class Solution { public: char firstUniqChar(string s) { unordered_map<char,int>mp; char ans=' '; vector<char>v; for(auto i:s) { if(!mp[i]) v.push_back(i); mp[i]++; } for(auto i:v) { if(mp[i]==1) return i; } return ans; } };
Day 6 search and backtracking algorithm (simple)
Sword finger Offer 32 - I. print binary tree from top to bottom
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> levelOrder(TreeNode* root) { queue<TreeNode*>que; que.push(root); vector<int>ans; if(!root) return ans; while(!que.empty()) { TreeNode *tmp=que.front(); que.pop(); if(tmp->left) que.push(tmp->left); if(tmp->right) que.push(tmp->right); ans.push_back(tmp->val); } return ans; } };
Sword finger Offer 32 - II Print binary tree II from top to bottom
Save the current node before putting the next layer of the current node into the queue
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { if(!root) return {}; queue<TreeNode*>que; que.push(root); vector<vector<int> >ans; while(!que.empty()) { vector<int>tmp; int sz=que.size(); for(int i=0;i<sz;i++) { TreeNode *now=que.front(); tmp.push_back(now->val); que.pop(); if(now->left) que.push(now->left); if(now->right) que.push(now->right); } ans.push_back(tmp); } return ans; } };
Sword finger Offer 32 - III. print binary tree III from top to bottom
It is the same as the previous question, but before saving to the final result, you need to judge the current level and turn it over
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { if(!root) return {}; queue<TreeNode*>que; que.push(root); vector<vector<int> >ans; while(!que.empty()) { vector<int>tmp; int sz=que.size(); for(int i=0;i<sz;i++) { TreeNode *now=que.front(); tmp.push_back(now->val); que.pop(); if(now->left) que.push(now->left); if(now->right) que.push(now->right); } if(ans.size()%2) reverse(tmp.begin(),tmp.end()); ans.push_back(tmp); } return ans; } };
Day 7 search and backtracking algorithm (simple)
Sword finger Offer 26 Substructure of tree
Start from A and iterate down. If there is A node equal to the root node of B, start A and B to recurse down at the same time
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSubStructure(TreeNode* A, TreeNode* B) { if(!A||!B) return false; if(dfs(A,B)) return true; return isSubStructure(A->left,B)||isSubStructure(A->right,B); } bool dfs(TreeNode *A,TreeNode *B) { if(!B) return true; if(!A) return false; if(A->val!=B->val) return false; return dfs(A->left,B->left)&&dfs(A->right,B->right); } };
Sword finger Offer 27 Image of binary tree
BFS
Using stack assisted traversal to realize the image of binary tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* mirrorTree(TreeNode* root) { if(!root) return root; stack<TreeNode*>st; st.push(root); while(!st.empty()) { TreeNode *node=st.top(); st.pop(); if(node->left) st.push(node->left); if(node->right) st.push(node->right); TreeNode *tmp=node->left; node->left=node->right; node->right=tmp; } return root; } };
DFS
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* mirrorTree(TreeNode* root) { if(!root) return root; TreeNode *node=root->left; root->left=mirrorTree(root->right); root->right=mirrorTree(node); return root; } };
Sword finger Offer 28 Symmetric binary tree
Traverse down the left subtree and right subtree at the same time
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSymmetric(TreeNode* root) { if(!root) return true; return dfs(root->left,root->right); } bool dfs(TreeNode *A,TreeNode *B) { if(!A&&!B) return true; if((!A||!B)||A->val!=B->val) return false; return dfs(A->left,B->right)&&dfs(A->right,B->left); } };
Day 8 dynamic planning (simple)
Sword finger Offer 10- I. Fibonacci sequence
Just don't use recursion
class Solution { public: int a[3]; const int mod=1e9+7; int fib(int n) { if(n<2) return n; a[0]=0;a[1]=1; for(int i=2;i<=n;i++) { a[2]=(a[1]%mod+a[0]%mod)%mod; a[0]=a[1];a[1]=a[2]; } return a[2]; } };
Sword finger Offer 10- II Frog jumping steps
\(dp[i]=dp[i-1]+dp[i-2]\)
class Solution { public: int dp[101]; const int mod=1e9+7; int numWays(int n) { dp[0]=1; dp[1]=1; dp[2]=2; for(int i=2;i<=n;i++) dp[i]=(dp[i-1]+dp[i-2])%mod; return dp[n]; } };
Sword finger Offer 63 Maximum profit of stock
Just keep updating the difference between the current element and the current minimum value
class Solution { public: int maxProfit(vector<int>& prices) { int sz=prices.size(); if(!sz) return 0; int m=prices[0]; int ans=0; for(int i=1;i<sz;i++) { m=min(prices[i],m); ans=max(ans,prices[i]-m); } return ans; } };