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

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ /*
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. 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.
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
queue<TreeNode*> node1,node2;
TreeNode* now1,*now2;
node1.push(root->left);
node2.push(root->right);
while(!node1.empty()&&!node2.empty()){
now1=node1.front();
node1.pop();
now2=node2.front();
node2.pop();
if (NULL==now1&&NULL==now2)
continue;
if (NULL==now1||NULL==now2)
return false;
if(now1->val!=now2->val){
return false;
}
node1.push(now1->left);
node1.push(now1->right);
node2.push(now2->right);
node2.push(now2->left);
}
return true;
}
};

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.

 class Solution {
public:
int maxProfit(vector<int>& prices) {
int Max1=,Max2=;
int ans=;
int n=prices.size();
vector<int> f1(n,);
vector<int> f2(n,);
for(int i=;i<n;i++){
int dis=prices[i]-prices[i-];
if(Max2<){
Max2=dis;
}
else Max2+=dis;
Max1=max(Max2,Max1);
f1[i]=Max1;
}
Max1=,Max2=;
for(int i=n-;i>=;i--){
int dis=prices[i]-prices[i+];
if(Max2>){
Max2=dis;
}
else Max2+=dis;
Max1=min(Max2,Max1);
f2[i]=Max1;
ans=max(ans,f1[i]-f2[i]);
}
return ans;
}
};

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.

 class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int,int> mp;
int tot=;
int ans=;
for(int i=;i<nums.size();i++){
if(!mp[nums[i]]) mp[nums[i]]=tot++;
}
for(int i=;i<nums.size();i++){
if(!mp[nums[i]]) continue;
int f1=nums[i]-,f2=nums[i]+;
while(mp[f1]) mp[f1]=,f1--;
while(mp[f2]) mp[f2]=,f2++;
ans=max(ans,f2-f1-);
}
return ans;
}
};

130

stackoverflow if you use dfs???????

 class Solution {
public:
int n,m;
struct Node{
int x,y;
};
int d[][]={,,,,,-,-,};
void bfs(int x,int y,vector<vector<char>>& board,vector<vector<int>>& vis){
vis[x][y]=;
queue<Node> q;
Node now,next;
now.x=x,now.y=y;
q.push(now);
while(!q.empty()){
now=q.front();
q.pop();
for(int i=;i<;i++){
next.x=now.x+d[i][];
next.y=now.y+d[i][];
if(next.x>=&next.x<n&&next.y>=&&next.y<m&&!vis[next.x][next.y]&&board[next.x][next.y]=='O'){
q.push(next);
vis[next.x][next.y]=;
}
}
}
}
void solve(vector<vector<char>>& board) {
if(board.size()==||board[].size()==) return;
n=board.size(),m=board[].size();
vector<vector<int>> vis(n,vector<int>(m,));
for(int j=;j<m;j++){
if(!vis[][j]&&board[][j]=='O') bfs(,j,board,vis);
if(!vis[n-][j]&&board[n-][j]=='O') bfs(n-,j,board,vis);
}
for(int j=;j<n;j++){
if(!vis[j][]&&board[j][]=='O') bfs(j,,board,vis);
if(!vis[j][m-]&&board[j][m-]=='O') bfs(j,m-,board,vis);
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(!vis[i][j]&&board[i][j]=='O') board[i][j]='X';
}
}
}
};

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

 class Solution {
public:
int minCut(string s) {
int len=s.length();
vector< vector<int> > dp(len+,vector<int>(len+,));
vector<int> sum(len,);
for(int i=;i<len;i++){
sum[i]=;
for(int j=;j<=i;j++){
if(s[i]==s[j]&&dp[j+][i-]==){
dp[j][i]=;
if(j==) sum[i]=;
else if(j->=){
sum[i]=min(sum[i],sum[j-]+);
}
}
else dp[j][i]=;
}
}
return sum[len-];
}
};

133 复制一幅图,作节点到节点的映射,防止环的情况

141,142判断链表中是否包含环,可以用map存,也可以用快慢指针,需要能流利的解释

 /*
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
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,
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.
*/

146 手动LRU,不会做

147. 148.linked list型 插入排序,归并排序,看discuss练好英文

149 判断最多有多少点在一条直线上,本来想记录斜率,但精度不够,顺便说一下double,float类型小数后面出现连续7个一样的数,就会自动四舍五入,用pair存储斜率就行

 /**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int gcd(int a, int b)
{
if(b == )
return a;
return gcd(b, a % b);
}
int maxPoints(vector<Point>& points){
Point a,b;
int ans=,tot=;
double k=;
Point w;
map<pair<int,int>,int> mp;
int n=points.size();
if(n==) return ;
for(int i=;i<n;i++){
int Max=;
tot=;
for(int j=i+;j<n;j++){
if(i==j) continue;
a=points[i];
b=points[j];
if(a.x==b.x&&a.y==b.y){
tot++;
continue;
}
pair<int,int> w;
if(a.x==b.x){
w.first=;
w.second=;
k=,mp[w]++;
}
else{
int gc=gcd((a.x-b.x),a.y-b.y);
w.first=(a.x-b.x)/gc;
w.second=(a.y-b.y)/gc;
mp[w]++;
}
Max=max(Max,mp[w]);
}
ans=max(ans,Max+tot);
mp.clear();
}
return ans+; }
};

150.水

 class Solution {
public:
int cal(string s){
int sum=;
int flag=;
int i=;
if(s[]=='-') i=,flag=-;
for(;i<s.length();i++){
sum=s[i]-''+sum*;
}
return sum*flag;
}
int evalRPN(vector<string>& tokens) {
stack<int> st;
int n=tokens.size();
int num1,num2,ans=;
for(int i=;i<n;i++){
string w=tokens[i];
if(w[]<=''&&w[]>=''||(w[]=='-'&&w[]<=''&&w[]>='')){
st.push(cal(w));
}
else{
num1=st.top();
st.pop();
num2=st.top();
st.pop();
if(w[]=='/'){
ans=num2/num1;
}
else if(w[]=='*'){
ans=num2*num1;
}
else if(w[]=='+'){
ans=num2+num1;
}
else if(w[]=='-'){
ans=num2-num1;
} st.push(ans);
}
}
return st.top();
}
};

151.水,代码比较丑陋

 void reverseWords(string &s) {
int n=s.size()+;
if(n==) return;
s+=' ';
string ans;
int pos=;
for(int i=;i<n;i++){
while(s[i]==' '){
i++;
}
if(i==n) break;
pos=i;
while(s[i]!=' '){
i++;
}
if(ans.length()==){
ans=s.substr(pos,i-pos);
}
else
ans=s.substr(pos,i-pos)+' '+ans;
pos=i+;
}
s=ans;
return;
}

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

 class Solution {
public:
int maxProduct(vector<int>& nums) {
int n=nums.size();
vector<int> Max(n+,);
vector<int> Min(n+,);
int ans=nums[];
Min[]=Max[]=nums[];
for(int i=;i<n;i++){
Max[i]=max(nums[i],max(Max[i-]*nums[i],Min[i-]*nums[i]));
Min[i]=min(nums[i],min(Min[i-]*nums[i],Max[i-]*nums[i]));
ans=max(ans,Max[i]);
}
return ans;
}
};

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

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

164 radix sort,基数排序

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

200.一堆bash题和一堆水题

Leetcode 记录(101~200)的更多相关文章

  1. Java基础知识强化04:判断101~200之间有多少素数

    1. 判断101~200之间有多少素数? package himi.hebao; /** * (1).编写函数isPrime()用来判断输入数据是否为素数 (2).遍历判断101~200之间的数据是否 ...

  2. java例题_02 101~200以内的素数

    1 /*2 [程序 2 输出素数] 2 题目:判断 101-200 之间有多少个素数,并输出所有素数. 3 程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除,则表明 ...

  3. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  4. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  5. LeetCode记录(1)——Array

    1.Two Sum naive 4.Median of Two Sorted Arrays 找两个已排序数组的中位数 直接数可以过,但很蠢,O(m+n)时间 class Solution { publ ...

  6. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. Leetcode 记录(1~100)

    5.回文串 几种方法: 暴力:枚举每一个字串,判断是否为回文串,复杂度O(n^3),暴力月莫不可取 dp:区间dp思想,O(n^2) 中心扩展:找每一个字符,然后往两边扩展,O(n^2) manach ...

  8. leetcode记录-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  9. LeetCode记录之9——Palindrome Number

    LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题.能力有限,先把easy的题目给刷完. Determine whether an integer is a palin ...

随机推荐

  1. axios 发送json数据

    var qs = require('qs'); axios.post(ajaxurl,qs.stringify({ username:'zhangsan', age:'18' })).then(fun ...

  2. IDEA加载项目的设置是tomcat

  3. windows上编译boost库

    要用xx库,编译boost时就指定--with-xx.例如: # 下载并解压boost_1.58 # 进入boost_1.58目录 bjam.exe toolset=msvc-14.0 --build ...

  4. 安装 jpegtran-cffi 使用 from jpegtran import JPEGImage

    Requirements CPython >=2.6 or >=3.3 or PyPy cffi >= 1.0 libturbojpeg with headers Install 1 ...

  5. [转]基于国家标准的 EndNote 输出样式模板 ----直接用endnote导入到word,不用自己一个个改参考文献了

    EndNote 相当于一个数据库,将添加/导入的文献存档.需要引用文献的时候就从中选择一个插入到文档中,EndNote 会自动给你编号.在文档末尾建立相应的参考文献列表.但是各种杂志.单位要求的文献著 ...

  6. Nginx Java 日志切割脚本

    Nginx日志切割脚本: #!/bin/bash ########################################################################### ...

  7. Nginx动静分离

    动静分离 Nginx动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路. ...

  8. 用groovy脚本进行每日工作的自动化【groovy】

    我们可以用groovy编写日常的批处理脚本,类似windows下的bat或者unix下的shell.其具体的编写方式非常简单,比如我们想要执行一个dir的命令,只要编写一个test.groovy,其中 ...

  9. 部署Tomcat及nginx负载均衡

    Web应用服务器的选择   (1)IBM的WebSphere及Oracle的WebLogic 性能高,但价格也高   (2)Tomcat 性价比高 Tomcat服务器是一个免费的开放源代码的Web应用 ...

  10. PHP使用Apache中的ab测试网站的压力性能及mpm介绍

    打开Apache安装的bin目录 shift+鼠标右键 复制粘贴以下代码->回车 ab -n 1000 -c 100 http://localhost/test.php 上例表示总共访问http ...