class Solution { public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { return pre_order(0, inorder.size() - 1, 0, inorder.size() - 1, preorder, inorder); }TreeNode *pre_order(int leftpre, int rightpre, int leftin, int rightin, vector<int> &pre, vect...
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };*/classSolution {public: TreeNode* buildTree(vector<int>& preorder, vector<int>&inorder) {if(preorder.size()==0||inorder.structize()==0)returnNULL;else{returnbuildCore(preorder,0,preorder.size()-1,inorder,0,inor...
代码 classSolution:defmaxAreaOfIsland(self,grid:List[List[int]])->int:# BFS+原地更改rows,cols=len(grid),len(grid[0])res=0forsrinrange(rows):forscinrange(cols):ifgrid[sr][sc]==1:# 找到一个尚未遍历过的土地, 开始BFSq=[(sr,sc)]# 将原有土地变成水, 更改其值为0, 避免重复遍历grid...
代码展示classSolution{int[]dx,dy;publicint[]executeInstructions(intn,int[]startPos,Strings){dx=new...
https://blog.csdn.net/qq_36915078/article/details/105730248 所以,当两个指针相遇后,令其中一个指针指向头结点,然后逐步移动,最后相遇的结点即为环起点。 classSolution{public:ListNode*detectCycle(ListNode*head){if(!head||!head->next)returnnullptr;ListNode*pl=head->next;ListNode*pf=head->next->next;...
classSolution{public:intsubarraySum(vector<int>&nums,int k){int answer=0,prefix=0;unordered_map<int,int>hashMap;hashMap[0]=1;for(int x:nums){prefix+=x;if(hashMap.find(prefix-k)!=hashMap.end())answer+=hashMap[prefix-k];hashMap[prefix]++;}returnanswer;}};...
classSolution{public:vector<vector<int>>indexPairs(string text,vector<string>&words){int i,len,maxlen=0;unordered_set<string>s;for(i=0;i<words.size();++i){s.insert(words[i]);maxlen=max(maxlen,(int)words[i].size());}vector<vector<int>>ans;for(i=0;i<text.size();++i){for(...
输入:nums=[]输出:[] 示例3: 输入:nums=[0]输出:[] 提示: 0 <= nums.length <= 3000 -105 <= nums[i] <= 105 classSolution:defthreeSum(self,nums:List[int])->List[List[int]]:nums.sort()res=[]foriinrange(len(nums)):ifiandnums[i]...
classSolution{ publicintcalculate(String s){ // 使用栈来储存字符串表达式中的数字 Stack<Integer> stack =newStack<Integer>; // 为了方便计算,所有的操作都视为加法操作 // 那么原先的减法操作就相当于是加一个负数 // 默认都是正数 intsign =1; ...
CSDN 博客:https://blog.csdn.net/lw_power; 微信公众号:力扣图解可能没有图(目前佛系运营)。 微信群与 QQ 群 如果需要一起刷题的朋友,可以加入微信群和 QQ 群。 微信群:每日一题打卡活动和加入微信群可以点 这里 。感谢 负雪明烛 建群; QQ 群:群号:812791932。感谢 二哥 建群。 我的LeetBook 这里给自己...