摘要:扫描线算法 每进入一个区间插一面旗子,出区间拔一面旗子。 相当于在平地上磊台阶,每次进入一个相同的区域,则加一阶台阶,每次出了一个会议室则减少一个台阶。最少使用的房间数,就是台阶的最大高度。并且通过同一个位置的加台阶和减少台阶,可以实现边界处的处理。 class Solution { public:
阅读全文
摘要:两个数组的相同字符之间可以连线且连线不能相交,乍一看就是动态规划。 再仔细想想,发现竟是最长公共子序列。 class Solution { public: int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) { int m =
阅读全文
摘要:解法一: map (哈希表) 时间复杂度: O(n) 空间复杂度:O(n) class Solution { public: int singleNumber(vector<int>& nums) { int n = nums.size(); map<int, int> mp; mp.clear()
阅读全文
摘要:class Solution { public: bool canCross(vector<int>& stones) { int n = stones.size(); vector<vector<int> > dp(n, vector<int>(n)); dp[0][0] = true; for(
阅读全文
摘要:常规解法: DP 求数组中子区间的最大乘积,对于乘法,我们需要注意,负数乘以负数,会变成正数,所以解这题的时候我们需要维护两个变量,当前的最大值,以及最小值,最小值可能为负数,但没准下一步乘以一个负数,当前的最大值就变成最小值,而最小值则变成最大值了。 class Solution { public
阅读全文
摘要:class MyQueue { stack<int> inStack, outStack; void in2out() { while(inStack.size()) { outStack.push(inStack.top()); inStack.pop(); } } public: /** Ini
阅读全文
摘要:class Solution { int cnt; int dis[12][12]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; public: int orangesRotting(vector<vector<int>>& grid)
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r
阅读全文
摘要:= > 将一棵BST转化为一个排序的双向循环链表,返回头结点,“head” 表示指向链表中有最小元素的节点。 由于是二叉搜索树,所以最左结点是最小元素的结点。 要有序,所以选择中序遍历。 /* // Definition for a Node. class Node { public: int va
阅读全文
摘要:二叉树的层序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left
阅读全文
摘要:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/ 请完成一个函数,输入一个二叉树,该函数输出它的镜像。 例如输入: 4 / \ 2 7 / \ / \1 3 6 9镜像输出: 4 / \ 7 2 / \ / \9 6 3
阅读全文
摘要:https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/ 注意,如果左右孩子L, R数值相同,再比较这两棵子树时,要比较的是哪两对结点。 /** * Definition for a binary tree node. * stru
阅读全文
摘要:题目要求: 已知二叉树的前序和中序遍历序列,输出该二叉树。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(
阅读全文