摘要: 题目链接:https://leetcode.cn/problems/different-ways-to-add-parentheses/ 个人题解: 首先是DFS深搜来解决 开一个数组,来存数字和字符,取出数字采用双指针即可 DFS递归两边,设置左右两个参数,分别计算 代码: class Solut 阅读全文
posted @ 2022-07-01 11:47 黑VS白-清墨 阅读(32) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/flood-fill/ 个人题解:经典flood-fill算法,DFS即可 代码: class Solution { public: int dx[4]={1,0,0,-1},dy[4]={0,1,-1,0}; void d 阅读全文
posted @ 2022-07-01 11:43 黑VS白-清墨 阅读(21) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/validate-binary-search-tree/ 个人题解:中序遍历数组有序为BST 代码: class Solution { public: bool isValidBST(TreeNode* root) { ve 阅读全文
posted @ 2022-06-30 09:03 黑VS白-清墨 阅读(16) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/binary-search/ 个人题解:最基础的二分 代码: class Solution { public: int search(vector<int>& nums, int target) { int l=0,r=nu 阅读全文
posted @ 2022-06-29 11:01 黑VS白-清墨 阅读(28) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/n-ary-tree-preorder-traversal/ 个人题解:DFS 代码: class Solution { public: vector<int> res; vector<int> preorder(Node* 阅读全文
posted @ 2022-06-28 08:32 黑VS白-清墨 阅读(25) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/ 个人题解:线性DP 代码: class Solution { public: int maxProfit(vector<int>& prices) { int 阅读全文
posted @ 2022-06-28 08:27 黑VS白-清墨 阅读(37) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/middle-of-the-linked-list/ 个人题解:双指针,快指针走到尾,慢指针就走到中间了 代码: class Solution { public: ListNode* middleNode(ListNode* 阅读全文
posted @ 2022-06-26 22:47 黑VS白-清墨 阅读(14) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/merge-two-sorted-lists/ 个人题解:设置虚拟头节点,分别取元素比大小,还有剩下的话加上去 代码: /** * Definition for singly-linked list. * struct Li 阅读全文
posted @ 2022-06-25 14:38 黑VS白-清墨 阅读(24) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/isomorphic-strings/ 个人题解:位置不一样等价于不是同构 代码: class Solution { public: bool isIsomorphic(string s, string t) { for(i 阅读全文
posted @ 2022-06-24 16:26 黑VS白-清墨 阅读(25) 评论(0) 推荐(0) 编辑
摘要: 第一题 题目链接:https://leetcode.cn/problems/running-sum-of-1d-array/ 个人题解:前缀和 代码: class Solution { public: vector<int> runningSum(vector<int>& nums) { for(i 阅读全文
posted @ 2022-06-23 23:56 黑VS白-清墨 阅读(6) 评论(0) 推荐(0) 编辑