没办法在自己向往的地方,做向往的事,毕其一生,都是白活,人生处处是路口,但总有几个是特别关键的。
我想把心脏剖开,披肝沥胆。但是不能,所以要夹起尾巴,把握机遇,迎接挑战
之前写的博客丢了,以后注意保存
LC297
class Codec { public: //Encode a tree to a single string. string serialize(TreeNode* root) { ostringstream out; serialize(root,out); //str() 返回ostringsteam里的临时值 return out.str(); }//es TreeNode* deserialize(string data) { istringstream in(data); return deserialize(in); } private: void serialize(TreeNode* root,ostringstream& out) { if(!root) { out<<"# "; return; } out<<root->val<<" "; serialize(root->left,out); serialize(root->right,out); } TreeNode* deserialize(istringstream& in) { string val; in>>val; if(val=="#") return nullptr; TreeNode* root=new TreeNode(stoi(val)); root->left=deserialize(in); root->right=deserialize(in); return root; } };
LC 298
298.Binary Tree Longest Consecutive Sequence(Medium)
Given a binary tree, find the length of the longest consecutive sequence path.
The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).
For example,
1
\
3
/ \
2 4
\
5
Longest consecutive sequence path is 3-4-5, so return 3.
2
\
3
/
2
/
1
Longest consecutive sequence path is 2-3,not3-2-1, so return 2.
一、题目
寻找树中最长连续序列,连续是指树的节点的值,且不能逆序。
DFS 不断找子节点
//LC298 #include<iostream> #include<math.h> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x):val(x),left(NULL),right(NULL){}; }; class Solution { public: int longestConsecutive(TreeNode* root) { if(root==NULL) return 0; helper(root,0,root->val); return res; } //因为之前是0直接赋值没有变量所以这里maxs不能加& void helper(TreeNode* root,int maxs,int target) { if(root==NULL) return; if(root->val==target) { ++maxs; }else { maxs=1; } res=max(res,maxs); helper(root->left,maxs,root->val+1); helper(root->right,maxs,root->val+1); } private: int res=0; }; int main() { TreeNode* root=new TreeNode(1); root->right=new TreeNode(3); root->right->right=new TreeNode(4); root->right->left=new TreeNode(2); root->right->right->right=new TreeNode(5); int ans=Solution().longestConsecutive(root); cout<<ans<<endl; return 0; }
BONUS:
vector删除元素三种方法:pop_back(),erase(),remove()
LC304
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
Example:
Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegion(2, 1, 4, 3) -> 8 sumRegion(1, 1, 2, 2) -> 11 sumRegion(1, 2, 2, 4) -> 12
Note:
- You may assume that the matrix does not change.
- There are many calls to sumRegion function.
- You may assume that row1 ≤ row2 and col1 ≤ col2.
这是两段代码
LC题解+cin测试
题解主函数:
初始化变量时要加*,不然会报错。用了*就是指针了要用->调用函数
cin测试 输入的数只要用空格分开了,都可以自动识别包括不同类别。
#include<iostream> #include<vector> using namespace std; class sumMatrix { public: //构造函数没有返回值 sumMatrix(vector<vector<int>>& matrix) { //prepare work sums.clear(); if(matrix.empty()) return; //initialize,包括对dp数组的初始化 int m=matrix.size(),n=matrix[0].size(); //对于已经定义了的vector,可以用赋值的方式进行初始化,便于直接用下标访问数组。数组类的问题,到底是从0-》N,还是1-》N+1,需要根据实际情况讨论 sums=vector<vector<int>>(m+1,vector<int>(n+1,0)); //form dp array,简化下标所以从1开始 for(int i=1;i<=m;++i) for(int j=1;j<=n;j++) //把下标改为1—》N+1,随之而来的是,对依赖或者原来数组的指代,仍需按0-》N范围,即像如下的matrix中需要-1; sums[i][j]=sums[i-1][j]+sums[i][j-1]-sums[i-1][j-1]+matrix[i-1][j-1]; } int sumRegion(int row1,int col1,int row2,int col2) { //一行过长,可以截取,不遇分号,终为一行,之前划分1-》N+1的问题再次显现,row col应该对照着写,小心问题。 return sums[row2+1][col2+1] -sums[row1][col2+1] -sums[row2+1][col1] +sums[row1][col1]; } private: //dp解法中存sums[i][j]代表以0,0为左上角,i,j为右下角的矩阵大小 vector<vector<int>> sums; }; int main() { int m,n; cin>>m>>n; vector<vector<int>> matrix(m,vector<int>(n,0)); matrix = {{3, 0, 1, 4, 2},{5, 6, 3, 2, 1},{1, 2, 0, 1, 5},{4, 1, 0, 1, 7},{1, 0, 3, 0, 5}}; for(int i=0;i<m;++i) for(int j=0;j<n;++j) cin>>matrix[i][j]; //初始化类一定要带星号,对象用箭头调用 sumMatrix* obj=new sumMatrix(matrix); int row1,col1,row2,col2; cin>>row1>>col1>>row2>>col2; int param_1=obj->sumRegion(row1,col1,row2,col2); cout<<param_1<<endl; return 0; } #include<bits/stdc++.h> using namespace std; int main() { int a,b,c,d; //输入的数值直接按空格分,cin会自动识别的,包括数据类型 double e; cin>>a>>b>>c>>d>>e; cout<<a<<b<<c<<d<<e<<endl; return 0; }
浙公网安备 33010602011771号