Skip to content

Commit 2e2f1ca

Browse files
committed
feat: add python and java solutions to lcci problem
添加《程序员面试金典》题解:面试题 02.01. 移除重复节点
1 parent 9fbcca7 commit 2e2f1ca

File tree

9 files changed

+260
-229
lines changed

9 files changed

+260
-229
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [面试题 02.01. 移除重复节点](https://leetcode-cn.com/problems/remove-duplicate-node-lcci/)
2+
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
6+
7+
**示例1:**
8+
9+
```
10+
输入:[1, 2, 3, 3, 2, 1]
11+
输出:[1, 2, 3]
12+
```
13+
14+
**示例2:**
15+
16+
```
17+
输入:[1, 1, 1, 1, 2]
18+
输出:[1, 2]
19+
```
20+
21+
**提示:**
22+
23+
- 链表长度在[0, 20000]范围内。
24+
- 链表元素在[0, 20000]范围内。
25+
26+
**进阶:**
27+
28+
如果不得使用临时缓冲区,该怎么解决?
29+
30+
## 解法
31+
<!-- 这里可写通用的实现逻辑 -->
32+
使用 set 暂存访问过的元素。
33+
34+
### Python3
35+
<!-- 这里可写当前语言的特殊实现逻辑 -->
36+
37+
```python
38+
# Definition for singly-linked list.
39+
# class ListNode:
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.next = None
43+
44+
class Solution:
45+
def removeDuplicateNodes(self, head: ListNode) -> ListNode:
46+
if head is None or head.next is None:
47+
return head
48+
cache = set()
49+
cache.add(head.val)
50+
cur, p = head, head.next
51+
while p:
52+
if p.val not in cache:
53+
cur.next = p
54+
cur = cur.next
55+
cache.add(p.val)
56+
p = p.next
57+
cur.next = None
58+
return head
59+
```
60+
61+
### Java
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```java
65+
/**
66+
* Definition for singly-linked list.
67+
* public class ListNode {
68+
* int val;
69+
* ListNode next;
70+
* ListNode(int x) { val = x; }
71+
* }
72+
*/
73+
class Solution {
74+
public ListNode removeDuplicateNodes(ListNode head) {
75+
if (head == null || head.next == null) {
76+
return head;
77+
}
78+
Set<Integer> s = new HashSet<>();
79+
s.add(head.val);
80+
ListNode p = head.next, cur = head;
81+
while (p != null) {
82+
if (!s.contains(p.val)) {
83+
cur.next = p;
84+
cur = cur.next;
85+
s.add(p.val);
86+
}
87+
p = p.next;
88+
}
89+
cur.next = null;
90+
return head;
91+
92+
}
93+
}
94+
```
95+
96+
### ...
97+
```
98+
99+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode removeDuplicateNodes(ListNode head) {
11+
if (head == null || head.next == null) {
12+
return head;
13+
}
14+
Set<Integer> s = new HashSet<>();
15+
s.add(head.val);
16+
ListNode p = head.next, cur = head;
17+
while (p != null) {
18+
if (!s.contains(p.val)) {
19+
cur.next = p;
20+
cur = cur.next;
21+
s.add(p.val);
22+
}
23+
p = p.next;
24+
}
25+
cur.next = null;
26+
return head;
27+
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def removeDuplicateNodes(self, head: ListNode) -> ListNode:
9+
if head is None or head.next is None:
10+
return head
11+
cache = set()
12+
cache.add(head.val)
13+
cur, p = head, head.next
14+
while p:
15+
if p.val not in cache:
16+
cur.next = p
17+
cur = cur.next
18+
cache.add(p.val)
19+
p = p.next
20+
cur.next = None
21+
return head
+16-98
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,28 @@
1-
## 两数相加
2-
### 题目描述
1+
# [2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers/)
32

4-
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
给出两个**非空**的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储**一位**数字。
6+
7+
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
58

6-
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
9+
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
10+
11+
**示例:**
712

8-
示例:
913
```
1014
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
1115
输出:7 -> 0 -> 8
1216
原因:342 + 465 = 807
1317
```
1418

15-
### 解法
16-
同时遍历两个链表,对应值相加(还有 quotient)求余数得到值并赋给新创建的结点。而商则用quotient存储,供下次相加。
17-
18-
#### Java
19-
20-
初始版本:
19+
## 解法
20+
<!-- 这里可写通用的实现逻辑 -->
21+
同时遍历两个链表,对应值相加(还有 quotient)求余数得到值并赋给新创建的结点。而商则用 quotient 存储,供下次相加。
2122

22-
```java
23-
/**
24-
* Definition for singly-linked list.
25-
* public class ListNode {
26-
* int val;
27-
* ListNode next;
28-
* ListNode(int x) { val = x; }
29-
* }
30-
*/
31-
class Solution {
32-
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
33-
ListNode res = new ListNode(-1);
34-
ListNode cur = res;
35-
int quotient = 0;
36-
int t = 0;
37-
while (l1 != null && l2 != null) {
38-
t = l1.val + l2.val + quotient;
39-
quotient = t / 10;
40-
ListNode node = new ListNode(t % 10);
41-
cur.next = node;
42-
l1 = l1.next;
43-
l2 = l2.next;
44-
cur = node;
45-
}
46-
47-
while (l1 != null) {
48-
t = l1.val + quotient;
49-
quotient = t / 10;
50-
ListNode node = new ListNode(t % 10);
51-
cur.next = node;
52-
l1 = l1.next;
53-
cur = node;
54-
}
55-
56-
while (l2 != null) {
57-
t = l2.val + quotient;
58-
quotient = t / 10;
59-
ListNode node = new ListNode(t % 10);
60-
cur.next = node;
61-
l2 = l2.next;
62-
cur = node;
63-
}
64-
65-
if (quotient != 0) {
66-
cur.next = new ListNode(quotient);
67-
cur = cur.next;
68-
}
69-
70-
return res.next;
71-
72-
}
73-
}
74-
```
23+
### Java
24+
<!-- 这里可写当前语言的特殊实现逻辑 -->
7525

76-
简化版本:
7726
```java
7827
/**
7928
* Definition for singly-linked list.
@@ -102,8 +51,8 @@ class Solution {
10251
}
10352
```
10453

105-
#### CPP
106-
```CPP
54+
### CPP
55+
```cpp
10756
class Solution {
10857
public:
10958
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
@@ -141,35 +90,4 @@ public:
14190
return head->next;
14291
}
14392
};
144-
145-
```
146-
147-
148-
# [题目](这里是题目链接,如:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/)
149-
150-
## 题目描述
151-
<!-- 这里写题目描述 -->
152-
153-
154-
## 解法
155-
<!-- 这里可写通用的实现逻辑 -->
156-
157-
158-
### Python3
159-
<!-- 这里可写当前语言的特殊实现逻辑 -->
160-
161-
```python
162-
163-
```
164-
165-
### Java
166-
<!-- 这里可写当前语言的特殊实现逻辑 -->
167-
168-
```java
169-
170-
```
171-
172-
### ...
173-
```
174-
17593
```

solution/0003.Longest Substring Without Repeating Characters/README.md

+17-33
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
## 无重复字符的最长子串
2-
### 题目描述
1+
# [3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
32

4-
给定一个字符串,找出不含有重复字符的**最长子串**的长度。
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
给定一个字符串,请你找出其中不含有重复字符的**最长子串**的长度。
6+
7+
**示例 1:**
58

6-
**示例 1:**
79
```
810
输入: "abcabcbb"
911
输出: 3
10-
解释: 无重复字符的最长子串是 "abc",其长度为 3。
12+
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
1113
```
1214

1315
**示例 2:**
16+
1417
```
1518
输入: "bbbbb"
1619
输出: 1
17-
解释: 无重复字符的最长子串是 "b",其长度为 1。
20+
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
1821
```
1922

2023
**示例 3:**
24+
2125
```
2226
输入: "pwwkew"
2327
输出: 3
24-
解释: 无重复字符的最长子串是 "wke",其长度为 3。
25-
请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串
28+
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
29+
  请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串
2630
```
2731

28-
### 解法
32+
## 解法
33+
<!-- 这里可写通用的实现逻辑 -->
2934
利用指针 `p`, `q`,初始指向字符串开头。遍历字符串,`q` 向右移动,若指向的字符在 map 中,说明出现了重复字符,此时,`p` 要在出现**重复字符的下一个位置** `map.get(chars[q]) + 1`**当前位置** `p` 之间取较大值,防止 `p` 指针回溯。循环的过程中,要将 chars[q] 及对应位置放入 map 中,也需要不断计算出`max``q - p + 1` 的较大值,赋给 `max`。最后输出 `max` 即可。
3035

36+
### Java
37+
<!-- 这里可写当前语言的特殊实现逻辑 -->
38+
3139
```java
3240
class Solution {
3341
public int lengthOfLongestSubstring(String s) {
@@ -55,30 +63,6 @@ class Solution {
5563
}
5664
```
5765

58-
# [题目](这里是题目链接,如:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/)
59-
60-
## 题目描述
61-
<!-- 这里写题目描述 -->
62-
63-
64-
## 解法
65-
<!-- 这里可写通用的实现逻辑 -->
66-
67-
68-
### Python3
69-
<!-- 这里可写当前语言的特殊实现逻辑 -->
70-
71-
```python
72-
73-
```
74-
75-
### Java
76-
<!-- 这里可写当前语言的特殊实现逻辑 -->
77-
78-
```java
79-
80-
```
81-
8266
### ...
8367
```
8468

0 commit comments

Comments
 (0)