武侯区网站建设,艺术类 网站建设方案,德阳建设银行招聘网站,重庆网站制作开发来记录几个注意事项 1.vector容器里利用find#xff08;#xff09;函数 不同于map#xff08;map有find方法#xff09;#xff0c;vector本身是没有find这一方法#xff0c;其find是依靠algorithm来实现的。 所以要包含头文件 #include iostream
#include … 来记录几个注意事项 1.vector容器里利用find函数 不同于mapmap有find方法vector本身是没有find这一方法其find是依靠algorithm来实现的。 所以要包含头文件 #include iostream
#include algorithm //find函数的调用需要包含algorithm这一头文件
#include vector另外返回类型并不是int 类型的索引 iterator迭代器类型的
auto inrootfind(vector.begin(),vector.end(),val)//假设在int类型的vector容器里找值为val的位置2.关于在vector容器里根据找寻到的位置进行切片前面为新的vector容器后面为一个新的vector容器 错误写法
vector inleftinorder(inorder.begin(),inroot); 这里并不是赋值操作利用赋值是不对的 正确写法
vectorint inleft(inorder.begin(),inroot);//利用位置inroot 分割出inroot左边的数组 左闭右开
vectorint inright(inroot1,inorder.end());//利用位置inroot 分割出inroot右边的数组 左闭右开解法递归
class Solution {//前序 中左右//中序 左中右
public:TreeNode* buildTree(vectorint preorder, vectorint inorder) {if(preorder.empty()||inorder.empty()) return nullptr;//1.根节点肯定是前序的第一个TreeNode* rootnew TreeNode(preorder[0]);//2.在中序遍历中找一下根节点的位置 记住这个函数find(begin(),end(),val)auto inrootfind(inorder.begin(),inorder.end(),preorder[0]);//3.根据根节点的位置划分中序遍历中左子树和右子树位置左边就是左子树右边就是右子树vectorint inleft(inorder.begin(),inroot);vectorint inright(inroot1,inorder.end());//4.根据中序遍历中左右子树的大小划分前序遍历数组int leftsizeinleft.size();vectorint preleft(preorder.begin()1,preorder.begin()1leftsize);//不能写成(1,leftsize1)vectorint preright(preorder.begin()1leftsize,preorder.end());//递归处理左右子树root-leftbuildTree(preleft,inleft);root-rightbuildTree(preright,inright);return root;}
};思路重新定义排序方式
sort(nums.begin(),nums.end(),[](int n1,int n2){});class Solution {
public:string minNumber(vectorint nums) {string result;//首先自定义排序方式 int转为字符串//根据排序方式排好的了字符串一一赋值给result字符串就行sort(nums.begin(),nums.end(),[](int n1,int n2){string s1to_string(n1),s2to_string(n2);return s1s2s2s1;//s1s2s2s1,说明s1更小更小的排前面});for(auto num:nums) resultto_string(num);return result;}
};class Solution {
public:int fib(int n) {if(n0) return 0;if(n1) return 1;int x0,y0,z1;for(int i2;in;i){xy;yz;z(xy)%1000000007;}return z;}
};