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. js 打开摄像头方法 (定制摄像头)

    var video = document.getElementById("video");if (navigator.mediaDevices && navigat ...

  2. servlet保存会话数据---利用隐藏域

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...

  3. excel 中怎么让两列姓名相同排序(转)

    如图,A列B列不动,C列和D列行值不变,以A列姓名为主让C列姓名和A列相同姓名的对齐(行),D行跟着C行不变. 在E1输入公式=MATCH(C1,A:A,0)然后下拉,接著选中C,D,E列,以E列为标 ...

  4. webstorm ps

    2018WebStorm注册码   2018-10-10 2018年08月22日 17:36:58 阳光明媚的味道 阅读数:6325   8月21日 http://webstorm.autoseasy ...

  5. 2. ELK 之kibana 简介、获取、安装

    简介 kibana是什么?简单理解就是一种可视化工具,比如日志记录之后的可视化操作工具,支持 折线图,饼状图,表格等,支持按时间维度等自定义维度角度 数据搜索.分析等等. 2.   获取 https: ...

  6. Python_冒泡排序

    从小到大的排序:(最前面的数和一步步和后面的数比较,如果大于则交换,如果不大于则继续循环) 方法1: data = [65, 1, 45, 77, 3, 9, 43, 23, 7, 53, 213, ...

  7. OSPF协议之详细图解

    OSPF是一种基于SPF算法的链路状态路由协议. 上图是在一个OSPF区域里面添入一台新的路由器的时候,OSPF协议的工作过程,如果你能非常详细的叙述出这张图的话,基本上OSPF协议的工作过程你就掌握 ...

  8. CSS3 鲜为人知的属性-webkit-tap-highlight-color的理解

    (一)-webkit-tap-highlight-color         这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...

  9. 获取Form表单数据转化成JSON对象

    $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() ...

  10. BZOJ3110 [Zjoi2013]K大数查询 树套树 线段树 整体二分 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3110 题意概括 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位 ...