文章分类 -  力扣算法每日练习

摘要:7.1组合问题 class Solution { public: vector<vector<int>> result; vector<int> path;//每一次的路径 void getpath(int n,int k,int startindex){ if(path.size()==k){// 阅读全文
posted @ 2023-11-17 13:32 Ref-rain- 阅读(6) 评论(0) 推荐(0)
摘要:6.22二叉搜索树的插入操作 class Solution { public: TreeNode* parent;//保存待插入结点的父节点 void traversal(TreeNode* bt,int val){ if(!bt){//bt根节点不存在,但是其父节点parent可能存在 TreeN 阅读全文
posted @ 2023-11-15 13:45 Ref-rain- 阅读(7) 评论(0) 推荐(0)
摘要:6.18二叉搜索树的最小绝对差 class Solution { public: vector<int> vec; void traversal(TreeNode * bt){//将二叉搜索树的所有结点放入容器中,中序遍历 if(!bt) return; traversal(bt->left); v 阅读全文
posted @ 2023-11-11 17:02 Ref-rain- 阅读(9) 评论(0) 推荐(0)
摘要:6.14最大二叉树 class Solution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { TreeNode* node = new TreeNode(0); if(nums.size()==1){//容器 阅读全文
posted @ 2023-11-10 18:35 Ref-rain- 阅读(7) 评论(0) 推荐(0)
摘要:6.10左叶子之和 class Solution { public: int sumOfLeftLeaves(TreeNode* root) { if(root==NULL) return 0; int leftval = sumOfLeftLeaves(root->left); if(root-> 阅读全文
posted @ 2023-11-10 11:08 Ref-rain- 阅读(23) 评论(0) 推荐(0)
摘要:6.6最小深度二叉树 class Solution {//后序遍历 public: int getMin(TreeNode* bt){ if(!bt) return 0; int m = getMin(bt->left); int n = getMin(bt->right); if(bt->left 阅读全文
posted @ 2023-11-07 18:16 Ref-rain- 阅读(8) 评论(0) 推荐(0)
摘要:6.2非递归遍历 class Solution {//前序 public: vector<int> preorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> st; if(root==NULL) return r 阅读全文
posted @ 2023-11-05 19:21 Ref-rain- 阅读(8) 评论(0) 推荐(0)
摘要:5.3删除相邻重复项元素 class Solution { public: string removeDuplicates(string s) { stack<int> st; for(char r : s){ if(st.empty()||r!=st.top())//栈空或者当前字符不等于上一个字 阅读全文
posted @ 2023-11-03 21:10 Ref-rain- 阅读(8) 评论(0) 推荐(0)
摘要:4.5左旋字符串 class Solution { public: string dynamicPassword(string password, int target) { reverse(password.begin(),password.begin()+target);//翻转前target个 阅读全文
posted @ 2023-11-02 15:07 Ref-rain- 阅读(9) 评论(0) 推荐(0)
摘要:4.1字符串翻转 class Solution { public: void reverseString(vector<char>& s) { for(int i=0,j=s.size()-1;i<s.size()/2;i++,j--){ swap(s[i],s[j]); } } }; 4.2翻转字 阅读全文
posted @ 2023-10-30 21:14 Ref-rain- 阅读(6) 评论(0) 推荐(0)
摘要:3.5四数之和 class Solution { public: int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { ​ unordered_map<in 阅读全文
posted @ 2023-10-29 20:46 Ref-rain- 阅读(5) 评论(0) 推荐(0)
摘要:3.1有效字母的异位词 class Solution { public: bool isAnagram(string s, string t) { ​ int record[26] = {0};//创建哈希表,数组 ​ int i = 0; ​ for(;i<s.size();i++) ​ reco 阅读全文
posted @ 2023-10-27 20:06 Ref-rain- 阅读(11) 评论(0) 推荐(0)
摘要:2.4反转链表 class Solution { public: ListNode* reverseList(ListNode* head) { ​ ListNode* pre = nullptr;创建一个空的头节点 ​ ListNode* cur = head; ​ ListNode* temp; 阅读全文
posted @ 2023-10-23 20:24 Ref-rain- 阅读(12) 评论(0) 推荐(0)
摘要:1.4.长度最小的子数组 滑动窗口。定义i,j作为左右窗口。若第一次满足sum值大于等于我们的目标值,则用sublength返回当前窗口的长度。然后左窗口收缩,再次判断是否大于等于目标值。满足就再次收缩。(sum-=nums[i++]) class Solution { public: int mi 阅读全文
posted @ 2023-10-23 20:03 Ref-rain- 阅读(10) 评论(0) 推荐(0)
摘要:10.21 1.二分查找 c++函数求数组的长度 .size() middle值作为下标,要放到数组中才能与target值进行比较 class Solution { public: int search(vector<int>& nums, int target) { ​ int middle; ​ 阅读全文
posted @ 2023-10-22 11:15 Ref-rain- 阅读(10) 评论(0) 推荐(0)