classSolution{public:int sum =0;boolhasPathSum(TreeNode* root,int targetSum){if(root ==nullptr){returnfalse;}
sum += root->val;if(root->left ==nullptr&& root->right ==nullptr){if(sum == targetSum){returntrue;}returnfalse;}if(root->left){bool left =hasPathSum(root->left, targetSum);if(left){returntrue;}
sum -= root->left->val;}if(root->right){bool right =hasPathSum(root->right, targetSum);if(right){returntrue;}
sum -= root->right->val;}returnfalse;}};
classSolution{public:
TreeNode*buildTree(vector<int>& preorder, vector<int>& inorder){if(preorder.size()==0){returnnullptr;}int val = preorder[0];int index =0;for(int i =0; i < inorder.size(); i++){if(inorder[i]== val){
index = i;}}
vector<int> left_preorder;
vector<int> right_preorder;
vector<int> left_inorder;
vector<int> right_inorder;for(int i =0; i < index; i++){
left_inorder.push_back(inorder[i]);}for(int i = index +1; i < inorder.size(); i++){
right_inorder.push_back(inorder[i]);}for(int i =1; i <= index; i++){
left_preorder.push_back(preorder[i]);}for(int i = index +1; i < preorder.size(); i++){
right_preorder.push_back(preorder[i]);}
TreeNode* node =newTreeNode(val);
node->left =buildTree(left_preorder, left_inorder);
node->right =buildTree(right_preorder, right_inorder);return node;}};
classSolution{public:
TreeNode*buildTree(vector<int>& inorder, vector<int>& postorder){if(postorder.size()==0){returnnullptr;}int val = postorder[postorder.size()-1];int index =0;for(int i =0; i < inorder.size(); i++){if(inorder[i]== val){
index = i;}}
TreeNode* node =newTreeNode(val);
vector<int> left_inorder;
vector<int> right_inorder;
vector<int> left_postorder;
vector<int> right_postorder;for(int i =0; i < index; i++){
left_inorder.push_back(inorder[i]);}for(int i = index +1; i < inorder.size(); i++){
right_inorder.push_back(inorder[i]);}for(int i =0; i < index; i++){
left_postorder.push_back(postorder[i]);}for(int i = index; i < postorder.size()-1; i++){
right_postorder.push_back(postorder[i]);}
node->left =buildTree(left_inorder, left_postorder);
node->right =buildTree(right_inorder, right_postorder);return node;}};
敲了段《仓颉语言实践》(张磊著)书上的代码,如下:
package test
//coding:utf-8struct Employee{var name:Stringprivate var realSalary:Float64public Employee(name:String,realSalary:Float64){this.namenamethis.realSalary…
省流:python(3.12.7) 和 pytorch(2.4.0)版本不配套 问题
起因是看到了这本书《Build a Large Language Model (From Scratch) 》,是2024年9月新出的, 作者 Sebastian Raschka,想要按照作者给出的步骤来手搓一个大语言模型&#…