mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -79,7 +79,7 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候,
|
||||
|
||||
所以 map中的存储结构为 {key:数据元素,value:数组元素对应的下标}。
|
||||
|
||||
在遍历数组的时候,只需要向map去查询是否有和目前遍历元素比配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。
|
||||
在遍历数组的时候,只需要向map去查询是否有和目前遍历元素匹配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。
|
||||
|
||||
过程如下:
|
||||
|
||||
|
@ -682,7 +682,33 @@ public class LinkNumbers
|
||||
|
||||
|
||||
|
||||
## 使用虚拟头结点解决链表翻转
|
||||
|
||||
> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈)
|
||||
|
||||
```java
|
||||
// 迭代方法:增加虚头结点,使用头插法实现链表翻转
|
||||
public static ListNode reverseList1(ListNode head) {
|
||||
// 创建虚头结点
|
||||
ListNode dumpyHead = new ListNode(-1);
|
||||
dumpyHead.next = null;
|
||||
// 遍历所有节点
|
||||
ListNode cur = head;
|
||||
while(cur != null){
|
||||
ListNode temp = cur.next;
|
||||
// 头插法
|
||||
cur.next = dumpyHead.next;
|
||||
dumpyHead.next = cur;
|
||||
cur = temp;
|
||||
}
|
||||
return dumpyHead.next;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 使用栈解决反转链表的问题
|
||||
|
||||
* 首先将所有的结点入栈
|
||||
* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。
|
||||
|
||||
@ -720,3 +746,4 @@ public ListNode reverseList(ListNode head) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -476,6 +476,7 @@ class Solution {
|
||||
## Python
|
||||
|
||||
> 递归法
|
||||
> 常量空间,递归产生的栈不算
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
@ -521,7 +522,9 @@ class Solution:
|
||||
```
|
||||
|
||||
|
||||
> 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性
|
||||
> 迭代法-中序遍历
|
||||
> 利用二叉搜索树特性,在历遍过程中更新结果,一次历遍
|
||||
> 但需要使用额外空间存储历遍的节点
|
||||
```python
|
||||
class Solution:
|
||||
def findMode(self, root: TreeNode) -> List[int]:
|
||||
|
Reference in New Issue
Block a user