Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8e0c77

Browse files
youngyangyang04sharky7pb
authored andcommitted
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 6d23362 + add9d53 commit e8e0c77

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

‎problems/0001.两数之和.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候,
7979

8080
所以 map中的存储结构为 {key:数据元素,value:数组元素对应的下标}。
8181

82-
在遍历数组的时候,只需要向map去查询是否有和目前遍历元素比配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。
82+
在遍历数组的时候,只需要向map去查询是否有和目前遍历元素匹配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。
8383

8484
过程如下:
8585

‎problems/0206.翻转链表.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,33 @@ public class LinkNumbers
682682

683683

684684

685+
## 使用虚拟头结点解决链表翻转
686+
687+
> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈)
688+
689+
```java
690+
// 迭代方法:增加虚头结点,使用头插法实现链表翻转
691+
public static ListNode reverseList1(ListNode head) {
692+
// 创建虚头结点
693+
ListNode dumpyHead = new ListNode(-1);
694+
dumpyHead.next = null;
695+
// 遍历所有节点
696+
ListNode cur = head;
697+
while(cur != null){
698+
ListNode temp = cur.next;
699+
// 头插法
700+
cur.next = dumpyHead.next;
701+
dumpyHead.next = cur;
702+
cur = temp;
703+
}
704+
return dumpyHead.next;
705+
}
706+
```
707+
708+
709+
685710
## 使用栈解决反转链表的问题
711+
686712
* 首先将所有的结点入栈
687713
* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。
688714

@@ -720,3 +746,4 @@ public ListNode reverseList(ListNode head) {
720746
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
721747
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
722748
</a>
749+

‎problems/0501.二叉搜索树中的众数.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ class Solution {
476476
## Python
477477

478478
> 递归法
479+
> 常量空间,递归产生的栈不算
479480
480481
```python
481482
# Definition for a binary tree node.
@@ -521,7 +522,9 @@ class Solution:
521522
```
522523

523524

524-
> 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性
525+
> 迭代法-中序遍历
526+
> 利用二叉搜索树特性,在历遍过程中更新结果,一次历遍
527+
> 但需要使用额外空间存储历遍的节点
525528
```python
526529
class Solution:
527530
def findMode(self, root: TreeNode) -> List[int]:

‎problems/回溯算法去重问题的另一种写法.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,38 @@ class Solution {
285285
}
286286
287287
```
288+
**90.子集II**
289+
```java
290+
class Solution {
291+
List<List<Integer>> reslut = new ArrayList<>();
292+
LinkedList<Integer> path = new LinkedList<>();
293+
294+
public List<List<Integer>> subsetsWithDup(int[] nums) {
295+
if(nums.length == 0){
296+
reslut.add(path);
297+
return reslut;
298+
}
299+
Arrays.sort(nums);
300+
backtracking(nums,0);
301+
return reslut;
302+
}
288303

304+
public void backtracking(int[] nums,int startIndex){
305+
reslut.add(new ArrayList<>(path));
306+
if(startIndex >= nums.length)return;
307+
HashSet<Integer> hashSet = new HashSet<>();
308+
for(int i = startIndex; i < nums.length; i++){
309+
if(hashSet.contains(nums[i])){
310+
continue;
311+
}
312+
hashSet.add(nums[i]);
313+
path.add(nums[i]);
314+
backtracking(nums,i+1);
315+
path.removeLast();
316+
}
317+
}
318+
}
319+
```
289320

290321
Python:
291322

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /