随笔分类 -  leetcode

摘要:class Solution { public: vector<int>p; void dfs(Node* root) { if(root==nullptr) return ; if (root->children.size() == 0){ p.push_back(root->val); retu 阅读全文
posted @ 2020-11-08 20:37 知道了呀~ 阅读(251) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: int result; int maxPathSum(TreeNode* root) { if (!root) return 0; result = root->val; dfs(root); return result; } //dfs递归求解以r 阅读全文
posted @ 2020-05-19 20:40 知道了呀~ 阅读(274) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: vector<int> inorderTraversal(TreeNode* root) { stack<TreeNode*> s; vector<int> v; TreeNode* now = root; while (now != NULL || 阅读全文
posted @ 2020-05-15 15:44 知道了呀~ 阅读(339) 评论(0) 推荐(0) 编辑
摘要:使用两个栈来协助完成二叉树的遍历操作。 不难发现,如果我们以“根->右->左”的顺序遍历二叉树,将结果压进栈中,弹栈的时候顺序就是“左->右->根”,也就是后序遍历的结果了。 而“根->右->左”的遍历顺序和先序遍历很像(先序遍历是“根->左->右”) 用stack1协助,对每个结点依次将“根->右 阅读全文
posted @ 2020-05-15 10:43 知道了呀~ 阅读(578) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; //注意判断根为空的情况 if (root == NULL) return res; queue<Tr 阅读全文
posted @ 2020-05-15 09:19 知道了呀~ 阅读(304) 评论(0) 推荐(0) 编辑
摘要:当一个元素要入栈时,我们取当前辅助栈的栈顶存储的最小值,与当前元素比较得出最小值,将这个最小值插入辅助栈中(min_stack.push(min(x,min_stack.top())) 当一个元素要出栈时,我们把辅助栈的栈顶元素也一并弹出; 在任意一个时刻,栈内元素的最小值就存储在辅助栈的栈顶元素中 阅读全文
posted @ 2020-05-13 15:11 知道了呀~ 阅读(241) 评论(0) 推荐(0) 编辑
摘要:dfs递归枚举每个点分片段的数字是否合法 class Solution { public: bool isValid(string ip) { int val = stoi(ip); if (val > 255) return false; if (ip.size() >= 2 && ip[0] = 阅读全文
posted @ 2020-05-13 14:41 知道了呀~ 阅读(265) 评论(0) 推荐(0) 编辑
摘要:先用栈判断括号匹配,在把所有匹配的括号标记为0,然后求最长的0序列 class Solution { public: int longestValidParentheses(string s) { int len = s.length(); int ans = 0; stack<int>p; for 阅读全文
posted @ 2020-05-13 10:30 知道了呀~ 阅读(246) 评论(0) 推荐(0) 编辑
摘要:因为要求时间和空间都要在O(n)内,所以用下标标记每个数是否出现,然后再顺序遍历,输出第一个下标未被标记的数,这种方法是不行的 应该遍历一边数组,判断 nums[nums[i] - 1] != nums[i](下标从0开始),如果不在,则交换num[i]和num[num[i]-1]。 我们的思路是把 阅读全文
posted @ 2020-05-07 16:44 知道了呀~ 阅读(152) 评论(0) 推荐(0) 编辑
摘要:中序遍历输出二叉树是一个递增的序列 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val 阅读全文
posted @ 2020-05-07 16:18 知道了呀~ 阅读(240) 评论(0) 推荐(0) 编辑
摘要:矩阵快速幂 #include <iostream> #include<string.h> #include<stdio.h> using namespace std; class Solution { public: typedef long long ll; const ll mod = 1000 阅读全文
posted @ 2020-05-06 17:05 知道了呀~ 阅读(299) 评论(0) 推荐(0) 编辑
摘要:先任意找数组中的一个元素a,采用快速排序将数组进行一次划分,即将小于a的元素放在其左侧,大于a的元素放在其右侧。然后判断元素a是否满足题目为第k小的数,满足则直接输出,否则判断下一次在哪一区间进行划分。 和快速排序的算法基本一样,只是当找到第k小的数之后就直接退出了,时间复杂度为O(n) //找第k 阅读全文
posted @ 2020-04-19 15:55 知道了呀~ 阅读(390) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示