Leetcode 记录(101~200)
Now, I want to just use English to explain the problem, it's about two month before the interview, so I have to practice my oral English more.
And I will write my explainations in my code in order to keep this passage looking comfortable.
101. Symmetric Tree
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 11 /* 12 I think it's a easy problem if you just works the problem one after another, but you probably don't know how to solve it with iterative way even if you have already solve it by recursive manner. You may think why should I use another way? However, the recursive way may be easy to understand, but it will cost more time at the same time and the stack can overflow. So it necessary to understand how to solve this problem iteratively. 13 14 First,we can image a line which is inseted between the left child and right child, the line is the mirror, every node can see a totally same node from this mirror. So we fold this tree and check. Actually, if we reverse one of the child tree and compare it with another, you can do it as same as tree-folding. 15 */ 16 class Solution { 17 public: 18 bool isSymmetric(TreeNode* root) { 19 if(root==NULL) return true; 20 queue<TreeNode*> node1,node2; 21 TreeNode* now1,*now2; 22 node1.push(root->left); 23 node2.push(root->right); 24 while(!node1.empty()&&!node2.empty()){ 25 now1=node1.front(); 26 node1.pop(); 27 now2=node2.front(); 28 node2.pop(); 29 if (NULL==now1&&NULL==now2) 30 continue; 31 if (NULL==now1||NULL==now2) 32 return false; 33 if(now1->val!=now2->val){ 34 return false; 35 } 36 node1.push(now1->left); 37 node1.push(now1->right); 38 node2.push(now2->right); 39 node2.push(now2->left); 40 } 41 return true; 42 } 43 };
105. Construct Binary Tree from Preorder and Inorder Traversal,Given preorder and inorder traversal of a tree, construct the binary tree.
It's a good problem and it's worth writing it again.
114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. I try to use recusive way to solve it, but I think it's too complex. Maybe I should write it a few days later.
115.It's truely a good problem. Though the difficulty is hard, you can easily do it when you understand what is dynamic programming. The first of discussion have a good explanation, and maybe you should explan this problem like that.
123.good problem
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. When you simplify this problem, you will find that you need to find two maxmium continuous subsequence in the array which is formed by the difference between the adjacent price. You can set an i which means the boundary of these two subsequence.
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int Max1=0,Max2=0; 5 int ans=0; 6 int n=prices.size(); 7 vector<int> f1(n,0); 8 vector<int> f2(n,0); 9 for(int i=1;i<n;i++){ 10 int dis=prices[i]-prices[i-1]; 11 if(Max2<0){ 12 Max2=dis; 13 } 14 else Max2+=dis; 15 Max1=max(Max2,Max1); 16 f1[i]=Max1; 17 } 18 Max1=0,Max2=0; 19 for(int i=n-2;i>=0;i--){ 20 int dis=prices[i]-prices[i+1]; 21 if(Max2>0){ 22 Max2=dis; 23 } 24 else Max2+=dis; 25 Max1=min(Max2,Max1); 26 f2[i]=Max1; 27 ans=max(ans,f1[i]-f2[i]); 28 } 29 return ans; 30 } 31 };
128.
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2]
,
The longest consecutive elements sequence is [1, 2, 3, 4]
. Return its length: 4
.
Your algorithm should run in O(n) complexity.
unorder_map is faster than map, but it will cost more memory.
1 class Solution { 2 public: 3 int longestConsecutive(vector<int>& nums) { 4 unordered_map<int,int> mp; 5 int tot=1; 6 int ans=0; 7 for(int i=0;i<nums.size();i++){ 8 if(!mp[nums[i]]) mp[nums[i]]=tot++; 9 } 10 for(int i=0;i<nums.size();i++){ 11 if(!mp[nums[i]]) continue; 12 int f1=nums[i]-1,f2=nums[i]+1; 13 while(mp[f1]) mp[f1]=0,f1--; 14 while(mp[f2]) mp[f2]=0,f2++; 15 ans=max(ans,f2-f1-1); 16 } 17 return ans; 18 } 19 };
130
stackoverflow if you use dfs???????
1 class Solution { 2 public: 3 int n,m; 4 struct Node{ 5 int x,y; 6 }; 7 int d[4][2]={1,0,0,1,0,-1,-1,0}; 8 void bfs(int x,int y,vector<vector<char>>& board,vector<vector<int>>& vis){ 9 vis[x][y]=1; 10 queue<Node> q; 11 Node now,next; 12 now.x=x,now.y=y; 13 q.push(now); 14 while(!q.empty()){ 15 now=q.front(); 16 q.pop(); 17 for(int i=0;i<4;i++){ 18 next.x=now.x+d[i][0]; 19 next.y=now.y+d[i][1]; 20 if(next.x>=0&next.x<n&&next.y>=0&&next.y<m&&!vis[next.x][next.y]&&board[next.x][next.y]=='O'){ 21 q.push(next); 22 vis[next.x][next.y]=1; 23 } 24 } 25 } 26 } 27 void solve(vector<vector<char>>& board) { 28 if(board.size()==0||board[0].size()==0) return; 29 n=board.size(),m=board[0].size(); 30 vector<vector<int>> vis(n,vector<int>(m,0)); 31 for(int j=0;j<m;j++){ 32 if(!vis[0][j]&&board[0][j]=='O') bfs(0,j,board,vis); 33 if(!vis[n-1][j]&&board[n-1][j]=='O') bfs(n-1,j,board,vis); 34 } 35 for(int j=0;j<n;j++){ 36 if(!vis[j][0]&&board[j][0]=='O') bfs(j,0,board,vis); 37 if(!vis[j][m-1]&&board[j][m-1]=='O') bfs(j,m-1,board,vis); 38 } 39 for(int i=0;i<n;i++){ 40 for(int j=0;j<m;j++){ 41 if(!vis[i][j]&&board[i][j]=='O') board[i][j]='X'; 42 } 43 } 44 } 45 };
132 大概意思就是判断最少有多少回文串
1 class Solution { 2 public: 3 int minCut(string s) { 4 int len=s.length(); 5 vector< vector<int> > dp(len+1,vector<int>(len+1,1)); 6 vector<int> sum(len,0); 7 for(int i=1;i<len;i++){ 8 sum[i]=999999; 9 for(int j=0;j<=i;j++){ 10 if(s[i]==s[j]&&dp[j+1][i-1]==1){ 11 dp[j][i]=1; 12 if(j==0) sum[i]=0; 13 else if(j-1>=0){ 14 sum[i]=min(sum[i],sum[j-1]+1); 15 } 16 } 17 else dp[j][i]=0; 18 } 19 } 20 return sum[len-1]; 21 } 22 };
133 复制一幅图,作节点到节点的映射,防止环的情况
141,142判断链表中是否包含环,可以用map存,也可以用快慢指针,需要能流利的解释
1 /* 2 my solution is like this: using two pointers, one of them one step at a time. another pointer each take two steps. Suppose the first meet at step k,the length of the Cycle is r. so..2k-k=nr,k=nr 3 Now, the distance between the start node of list and the start node of cycle is s. the distance between the start of list and the first meeting node is k(the pointer which wake one step at a time waked k steps).the distance between the start node of cycle and the first meeting node is m, so...s=k-m, 4 s=nr-m=(n-1)r+(r-m),here we takes n = 1..so, using one pointer start from the start node of list, another pointer start from the first meeting node, all of them wake one step at a time, the first time they meeting each other is the start of the cycle. 5 */
146 手动LRU,不会做
147. 148.linked list型 插入排序,归并排序,看discuss练好英文
149 判断最多有多少点在一条直线上,本来想记录斜率,但精度不够,顺便说一下double,float类型小数后面出现连续7个一样的数,就会自动四舍五入,用pair存储斜率就行
1 /** 2 * Definition for a point. 3 * struct Point { 4 * int x; 5 * int y; 6 * Point() : x(0), y(0) {} 7 * Point(int a, int b) : x(a), y(b) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int gcd(int a, int b) 13 { 14 if(b == 0) 15 return a; 16 return gcd(b, a % b); 17 } 18 int maxPoints(vector<Point>& points){ 19 Point a,b; 20 int ans=0,tot=0; 21 double k=0; 22 Point w; 23 map<pair<int,int>,int> mp; 24 int n=points.size(); 25 if(n==0) return 0; 26 for(int i=0;i<n;i++){ 27 int Max=0; 28 tot=0; 29 for(int j=i+1;j<n;j++){ 30 if(i==j) continue; 31 a=points[i]; 32 b=points[j]; 33 if(a.x==b.x&&a.y==b.y){ 34 tot++; 35 continue; 36 } 37 pair<int,int> w; 38 if(a.x==b.x){ 39 w.first=999999; 40 w.second=0; 41 k=999999,mp[w]++; 42 } 43 else{ 44 int gc=gcd((a.x-b.x),a.y-b.y); 45 w.first=(a.x-b.x)/gc; 46 w.second=(a.y-b.y)/gc; 47 mp[w]++; 48 } 49 Max=max(Max,mp[w]); 50 } 51 ans=max(ans,Max+tot); 52 mp.clear(); 53 } 54 return ans+1; 55 56 } 57 };
150.水
1 class Solution { 2 public: 3 int cal(string s){ 4 int sum=0; 5 int flag=1; 6 int i=0; 7 if(s[0]=='-') i=1,flag=-1; 8 for(;i<s.length();i++){ 9 sum=s[i]-'0'+sum*10; 10 } 11 return sum*flag; 12 } 13 int evalRPN(vector<string>& tokens) { 14 stack<int> st; 15 int n=tokens.size(); 16 int num1,num2,ans=0; 17 for(int i=0;i<n;i++){ 18 string w=tokens[i]; 19 if(w[0]<='9'&&w[0]>='0'||(w[0]=='-'&&w[1]<='9'&&w[1]>='0')){ 20 st.push(cal(w)); 21 } 22 else{ 23 num1=st.top(); 24 st.pop(); 25 num2=st.top(); 26 st.pop(); 27 if(w[0]=='/'){ 28 ans=num2/num1; 29 } 30 else if(w[0]=='*'){ 31 ans=num2*num1; 32 } 33 else if(w[0]=='+'){ 34 ans=num2+num1; 35 } 36 else if(w[0]=='-'){ 37 ans=num2-num1; 38 } 39 40 st.push(ans); 41 } 42 } 43 return st.top(); 44 } 45 };
151.水,代码比较丑陋
1 void reverseWords(string &s) { 2 int n=s.size()+1; 3 if(n==1) return; 4 s+=' '; 5 string ans; 6 int pos=0; 7 for(int i=0;i<n;i++){ 8 while(s[i]==' '){ 9 i++; 10 } 11 if(i==n) break; 12 pos=i; 13 while(s[i]!=' '){ 14 i++; 15 } 16 if(ans.length()==0){ 17 ans=s.substr(pos,i-pos); 18 } 19 else 20 ans=s.substr(pos,i-pos)+' '+ans; 21 pos=i+1; 22 } 23 s=ans; 24 return; 25 }
152.连续最大积,蠢哭了我
1 class Solution { 2 public: 3 int maxProduct(vector<int>& nums) { 4 int n=nums.size(); 5 vector<int> Max(n+1,0); 6 vector<int> Min(n+1,0); 7 int ans=nums[0]; 8 Min[0]=Max[0]=nums[0]; 9 for(int i=1;i<n;i++){ 10 Max[i]=max(nums[i],max(Max[i-1]*nums[i],Min[i-1]*nums[i])); 11 Min[i]=min(nums[i],min(Min[i-1]*nums[i],Max[i-1]*nums[i])); 12 ans=max(ans,Max[i]); 13 } 14 return ans; 15 } 16 };
153,154很棒的两题。对于一个折过的有序序列进行二分查找
162 给一段数字,寻找peak元素,即比左右元素大的数,您的好友智商已下线。。。
164 radix sort,基数排序
188 刷新了leetcode的新难度,感觉目前最难,dp,交易来交易去很麻烦
200.一堆bash题和一堆水题