mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
@ -17,6 +17,11 @@
|
||||
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
|
||||
</p>
|
||||
<p align="center">
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ" target="_blank">
|
||||
<img src="./pics/知识星球.png" width="600"/>
|
||||
</a>
|
||||
|
||||
|
||||
# LeetCode 刷题攻略
|
||||
|
||||
|
@ -193,40 +193,6 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
使用栈来模拟后序遍历依然可以
|
||||
|
||||
```C++
|
||||
class Solution {
|
||||
public:
|
||||
int maxDepth(TreeNode* root) {
|
||||
stack<TreeNode*> st;
|
||||
if (root != NULL) st.push(root);
|
||||
int depth = 0;
|
||||
int result = 0;
|
||||
while (!st.empty()) {
|
||||
TreeNode* node = st.top();
|
||||
if (node != NULL) {
|
||||
st.pop();
|
||||
st.push(node); // 中
|
||||
st.push(NULL);
|
||||
depth++;
|
||||
if (node->right) st.push(node->right); // 右
|
||||
if (node->left) st.push(node->left); // 左
|
||||
|
||||
} else {
|
||||
st.pop();
|
||||
node = st.top();
|
||||
st.pop();
|
||||
depth--;
|
||||
}
|
||||
result = result > depth ? result : depth;
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
|
@ -22,13 +22,13 @@ https://leetcode-cn.com/problems/ransom-note/
|
||||
|
||||
你可以假设两个字符串均只含有小写字母。
|
||||
|
||||
canConstruct("a", "b") -> false
|
||||
canConstruct("aa", "ab") -> false
|
||||
canConstruct("aa", "aab") -> true
|
||||
canConstruct("a", "b") -> false
|
||||
canConstruct("aa", "ab") -> false
|
||||
canConstruct("aa", "aab") -> true
|
||||
|
||||
## 思路
|
||||
|
||||
这道题目和[242.有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)很像,[242.有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)相当于求 字符串a 和 字符串b 是否可以相互组成 ,而这道题目是求 字符串a能否组成字符串b,而不用管字符串b 能不能组成字符串a。
|
||||
这道题目和[242.有效的字母异位词](https://mp.weixin.qq.com/s/ffS8jaVFNUWyfn_8T31IdA)很像,[242.有效的字母异位词](https://mp.weixin.qq.com/s/ffS8jaVFNUWyfn_8T31IdA)相当于求 字符串a 和 字符串b 是否可以相互组成 ,而这道题目是求 字符串a能否组成字符串b,而不用管字符串b 能不能组成字符串a。
|
||||
|
||||
本题判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成,但是这里需要注意两点。
|
||||
|
||||
@ -75,7 +75,7 @@ public:
|
||||
|
||||
依然是数组在哈希法中的应用。
|
||||
|
||||
一些同学可能想,用数组干啥,都用map完事了,**其实在本题的情况下,使用map的空间消耗要比数组大一些的,因为map要维护红黑树或者哈希表,而且还要做哈希函数。 所以数组更加简单直接有效!**
|
||||
一些同学可能想,用数组干啥,都用map完事了,**其实在本题的情况下,使用map的空间消耗要比数组大一些的,因为map要维护红黑树或者哈希表,而且还要做哈希函数,是费时的!数据量大的话就能体现出来差别了。 所以数组更加简单直接有效!**
|
||||
|
||||
代码如下:
|
||||
|
||||
|
@ -32,11 +32,11 @@
|
||||
|
||||
看如下两个链表,目前curA指向链表A的头结点,curB指向链表B的头结点:
|
||||
|
||||
v
|
||||

|
||||
|
||||
我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置,如图:
|
||||
|
||||

|
||||

|
||||
|
||||
此时我们就可以比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到焦点。
|
||||
|
||||
|
Reference in New Issue
Block a user