博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 记录(101~200)
阅读量:4603 次
发布时间:2019-06-09

本文共 9780 字,大约阅读时间需要 32 分钟。

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
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 };
View Code

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
& prices) { 4 int Max1=0,Max2=0; 5 int ans=0; 6 int n=prices.size(); 7 vector
f1(n,0); 8 vector
f2(n,0); 9 for(int i=1;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 };
View Code

 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
& nums) { 4 unordered_map
mp; 5 int tot=1; 6 int ans=0; 7 for(int i=0;i
View Code

 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
>& board,vector
>& vis){ 9 vis[x][y]=1;10 queue
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
=0&&next.y
>& board) {28 if(board.size()==0||board[0].size()==0) return;29 n=board.size(),m=board[0].size();30 vector
> vis(n,vector
(m,0));31 for(int j=0;j
View Code

 132 大概意思就是判断最少有多少回文串

1 class Solution { 2 public: 3     int minCut(string s) { 4         int len=s.length(); 5         vector< vector
> dp(len+1,vector
(len+1,1)); 6 vector
sum(len,0); 7 for(int i=1;i
=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 };
View Code

 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=nr3 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 */
View Code

 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
& points){19 Point a,b;20 int ans=0,tot=0;21 double k=0;22 Point w;23 map
,int> mp;24 int n=points.size();25 if(n==0) return 0;26 for(int i=0;i
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 };
View Code

 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
& tokens) {14 stack
st;15 int n=tokens.size();16 int num1,num2,ans=0;17 for(int i=0;i
='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 };
View Code

 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
View Code

 152.连续最大积,蠢哭了我

1 class Solution { 2 public: 3     int maxProduct(vector
& nums) { 4 int n=nums.size(); 5 vector
Max(n+1,0); 6 vector
Min(n+1,0); 7 int ans=nums[0]; 8 Min[0]=Max[0]=nums[0]; 9 for(int i=1;i
View Code

153,154很棒的两题。对于一个折过的有序序列进行二分查找

162 给一段数字,寻找peak元素,即比左右元素大的数,您的好友智商已下线。。。

164 radix sort,基数排序

188 刷新了leetcode的新难度,感觉目前最难,dp,交易来交易去很麻烦

200.一堆bash题和一堆水题

转载于:https://www.cnblogs.com/cnblogs321114287/p/6945839.html

你可能感兴趣的文章
Markdown-写作必备
查看>>
关于在Java中 a!=a 值为真的解释(摘抄)
查看>>
C#串口小助手
查看>>
详解定位与定位应用
查看>>
【前端开发】 5分钟创建 Mock Server
查看>>
java 从键盘录入的三种方法
查看>>
使用jQuery和YQL,以Ajax方式加载外部内容
查看>>
pyspider 示例
查看>>
电路板工艺中的NPTH和PTH
查看>>
JNI实现JAVA和C++互相调用
查看>>
JAVA 笔记(一)
查看>>
js 循环读取 json的值
查看>>
c# 范型Dictionary实用例子
查看>>
C#实现动态页面静态化
查看>>
可选参数、命名参数、.NET的特殊类型、特性
查看>>
利用CGLib实现动态代理实现Spring的AOP
查看>>
面试之SQL(1)--选出选课数量>=2的学号
查看>>
IIS处理并发请求时出现的问题
查看>>
数学作业
查看>>
使用pycharm开发web——django2.1.5(二)创建一个app并做一些配置
查看>>