Lambda
/* [] {}(); 匿名函数 一次性 语法开销
捕获字段:[=,&,i,&j] https://www.cnblogs.com/findumars/p/8062299.html
变量列表:(int a,int b) 你需要传递的参数
捕获的变量可否修改
异常设定
返回类型
函数体:{}
调用:(1,2);
*/
class Test{
public:
void hello(){cout <<"\n4 "<< "捕获了 this 指针!\n";};
void lambda(){
auto fun = [this]{ // 捕获了 this 指针
this->hello();
}; // 这里 this 调用的就是 class Test 的对象了
fun();
}
};
int main() {
int i=1;
int *j=&i;
string s="Hello World!";
//1 简单调用(全部拷贝=/全部引用& 必须放在 指定拷贝/引用前面)
//1.1
auto fun=[=,&j](int a,int b)->bool{cout<<"1.1 "<<i<<' '<<j<<' '<<s<<' '<<a<<' '<<b<<endl;return true;};
fun(3,4);
//1.2
[&,i](int k){cout<<"1.2 "<<i+*j<<endl;}(100);
//2 for_each
vector<int>v={1,2,3,4,9,8,7,5};
int sum=0;
for_each(v.begin(),v.end(),[&sum](int x){sum+=x;});
cout<<"2 "<<"sum:"<<sum<<endl;
//3 sort
sort(v.begin(),v.end(),[](int a,int b){return a>b;});
cout<<"3 ";
for_each(v.begin(),v.end(),[](int x){cout<<x<<' ';});
//4 类 this
Test t;
t.lambda();
return 0;
}
图
给定一个整数矩阵,找出最长递增路径的长度。
对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
示例 1:
输入: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]。
示例 2:
输入: nums =
[
[3,4,5],
[3,2,6],
[2,2,1]
]
输出: 4
解释: 最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。
class Solution {
vector<int>dx={1,0,-1,0},dy={0,1,0,-1};
public:
//dfs+记忆化
int longestIncreasingPath(vector<vector<int>>& matrix){
int m,n,res=0;
if((m=matrix.size())<1||(n=matrix[0].size())<1)return 0;
vector<vector<int>>mem(m,vector<int>(n,-1));
for(int i=0;i<m;+&