Skip to content

Commit 20d261b

Browse files
committed
feat: add solutions to lc problems: No.0817, 1715
1 parent 6e9056e commit 20d261b

File tree

7 files changed

+174
-73
lines changed

7 files changed

+174
-73
lines changed

solution/0800-0899/0817.Linked List Components/README.md

+59-3
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,83 @@ G = [0, 3, 1, 4]
4343
<li><code>G</code> 是链表中所有结点的值的一个子集.</li>
4444
</ul>
4545

46-
4746
## 解法
4847

4948
<!-- 这里可写通用的实现逻辑 -->
5049

50+
定义 pre 表示是否可加 1,初始为 true。
51+
52+
遍历链表各个结点:
53+
54+
- 若当前结点值在 nums 中,并且当前为可加 1 的状态,则 `res++`,并且将 pre 状态置为 false;
55+
- 若当前结点值不在 nums 中,则将 pre 置为 true,表示可加 1。
56+
57+
最后返回 res 即可。
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**
5462

5563
<!-- 这里可写当前语言的特殊实现逻辑 -->
5664

5765
```python
58-
66+
# Definition for singly-linked list.
67+
# class ListNode:
68+
# def __init__(self, val=0, next=None):
69+
# self.val = val
70+
# self.next = next
71+
class Solution:
72+
def numComponents(self, head: ListNode, nums: List[int]) -> int:
73+
s = set(nums)
74+
res, pre = 0, True
75+
while head:
76+
if head.val in s:
77+
if pre:
78+
res += 1
79+
pre = False
80+
else:
81+
pre = True
82+
head = head.next
83+
return res
5984
```
6085

6186
### **Java**
6287

6388
<!-- 这里可写当前语言的特殊实现逻辑 -->
6489

6590
```java
66-
91+
/**
92+
* Definition for singly-linked list.
93+
* public class ListNode {
94+
* int val;
95+
* ListNode next;
96+
* ListNode() {}
97+
* ListNode(int val) { this.val = val; }
98+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
99+
* }
100+
*/
101+
class Solution {
102+
public int numComponents(ListNode head, int[] nums) {
103+
Set<Integer> s = new HashSet<>();
104+
for (int num : nums) {
105+
s.add(num);
106+
}
107+
int res = 0;
108+
boolean pre = true;
109+
while (head != null) {
110+
if (s.contains(head.val)) {
111+
if (pre) {
112+
++res;
113+
pre = false;
114+
}
115+
} else {
116+
pre = true;
117+
}
118+
head = head.next;
119+
}
120+
return res;
121+
}
122+
}
67123
```
68124

69125
### **...**

solution/0800-0899/0817.Linked List Components/README_EN.md

+50-20
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66

77
<p>We are given&nbsp;<code>head</code>,&nbsp;the head node of a linked list containing&nbsp;<strong>unique integer values</strong>.</p>
88

9-
10-
119
<p>We are also given the list&nbsp;<code>G</code>, a subset of the values in the linked list.</p>
1210

13-
14-
1511
<p>Return the number of connected components in <code>G</code>, where two values are connected if they appear consecutively in the linked list.</p>
1612

17-
18-
1913
<p><strong>Example 1:</strong></p>
2014

21-
22-
2315
<pre>
2416

2517
<strong>Input:</strong>
@@ -36,12 +28,8 @@ G = [0, 1, 3]
3628

3729
</pre>
3830

39-
40-
4131
<p><strong>Example 2:</strong></p>
4232

43-
44-
4533
<pre>
4634

4735
<strong>Input:</strong>
@@ -58,35 +46,77 @@ G = [0, 3, 1, 4]
5846

5947
</pre>
6048

61-
62-
6349
<p><strong>Note: </strong></p>
6450

65-
66-
6751
<ul>
6852
<li>If&nbsp;<code>N</code>&nbsp;is the&nbsp;length of the linked list given by&nbsp;<code>head</code>,&nbsp;<code>1 &lt;= N &lt;= 10000</code>.</li>
6953
<li>The value of each node in the linked list will be in the range<code> [0, N - 1]</code>.</li>
7054
<li><code>1 &lt;= G.length &lt;= 10000</code>.</li>
7155
<li><code>G</code> is a subset of all values in the linked list.</li>
7256
</ul>
7357

74-
75-
7658
## Solutions
7759

7860
<!-- tabs:start -->
7961

8062
### **Python3**
8163

8264
```python
83-
65+
# Definition for singly-linked list.
66+
# class ListNode:
67+
# def __init__(self, val=0, next=None):
68+
# self.val = val
69+
# self.next = next
70+
class Solution:
71+
def numComponents(self, head: ListNode, nums: List[int]) -> int:
72+
s = set(nums)
73+
res, pre = 0, True
74+
while head:
75+
if head.val in s:
76+
if pre:
77+
res += 1
78+
pre = False
79+
else:
80+
pre = True
81+
head = head.next
82+
return res
8483
```
8584

8685
### **Java**
8786

8887
```java
89-
88+
/**
89+
* Definition for singly-linked list.
90+
* public class ListNode {
91+
* int val;
92+
* ListNode next;
93+
* ListNode() {}
94+
* ListNode(int val) { this.val = val; }
95+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
96+
* }
97+
*/
98+
class Solution {
99+
public int numComponents(ListNode head, int[] nums) {
100+
Set<Integer> s = new HashSet<>();
101+
for (int num : nums) {
102+
s.add(num);
103+
}
104+
int res = 0;
105+
boolean pre = true;
106+
while (head != null) {
107+
if (s.contains(head.val)) {
108+
if (pre) {
109+
++res;
110+
pre = false;
111+
}
112+
} else {
113+
pre = true;
114+
}
115+
head = head.next;
116+
}
117+
return res;
118+
}
119+
}
90120
```
91121

92122
### **...**

solution/0800-0899/0817.Linked List Components/Solution.java

+19-25
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,30 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
10-
public int numComponents(ListNode head, int[] G) {
11-
if (head == null || G == null) {
12-
return 0;
12+
public int numComponents(ListNode head, int[] nums) {
13+
Set<Integer> s = new HashSet<>();
14+
for (int num : nums) {
15+
s.add(num);
1316
}
14-
Set<Integer> set = new HashSet<>();
15-
for (int val : G) {
16-
set.add(val);
17-
}
18-
int n = G.length;
19-
ListNode cur = head;
20-
int cnt = 0;
21-
boolean flag = false;
22-
while (cur != null) {
23-
while (cur != null && set.contains(cur.val)) {
24-
flag = true;
25-
cur = cur.next;
26-
}
27-
if (flag) {
28-
++cnt;
29-
flag = false;
30-
}
31-
32-
if (cur != null) {
33-
cur = cur.next;
17+
int res = 0;
18+
boolean pre = true;
19+
while (head != null) {
20+
if (s.contains(head.val)) {
21+
if (pre) {
22+
++res;
23+
pre = false;
24+
}
25+
} else {
26+
pre = true;
3427
}
28+
head = head.next;
3529
}
36-
return cnt;
30+
return res;
3731
}
3832
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
7-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
86
class Solution:
9-
def numComponents(self, head, G):
10-
"""
11-
:type head: ListNode
12-
:type G: List[int]
13-
:rtype: int
14-
"""
15-
dic = set(G)
16-
ans = 0
17-
flag = 0
7+
def numComponents(self, head: ListNode, nums: List[int]) -> int:
8+
s = set(nums)
9+
res, pre = 0, True
1810
while head:
19-
if head.val not in dic:
20-
if flag == 1:
21-
ans += 1
22-
flag = 0
11+
if head.val in s:
12+
if pre:
13+
res += 1
14+
pre = False
2315
else:
24-
flag = 1
16+
pre = True
2517
head = head.next
26-
else:
27-
if flag == 1:
28-
ans += 1
29-
return ans
18+
return res

solution/1700-1799/1715.Count Apples and Oranges/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,23 @@ Chests 表:
9090

9191
<!-- 这里可写通用的实现逻辑 -->
9292

93+
`LEFT JOIN` + `IFNULL`”实现。
94+
9395
<!-- tabs:start -->
9496

9597
### **SQL**
9698

9799
```sql
98-
100+
# Write your MySQL query statement below
101+
SELECT
102+
SUM(IFNULL(b.apple_count, 0) + IFNULL(c.apple_count, 0)) AS apple_count,
103+
SUM(IFNULL(b.orange_count, 0) + IFNULL(c.orange_count, 0)) AS orange_count
104+
FROM
105+
Boxes b
106+
LEFT JOIN
107+
Chests c
108+
ON
109+
b.chest_id = c.chest_id;
99110
```
100111

101112
<!-- tabs:end -->

solution/1700-1799/1715.Count Apples and Oranges/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,21 @@ Total number of oranges = 15 + 25 + 8 + 28 + 15 + 15 + 17 = 123
9393

9494
<!-- tabs:start -->
9595

96+
`LEFT JOIN` and `IFNULL`.
97+
9698
### **SQL**
9799

98100
```sql
99-
101+
# Write your MySQL query statement below
102+
SELECT
103+
SUM(IFNULL(b.apple_count, 0) + IFNULL(c.apple_count, 0)) AS apple_count,
104+
SUM(IFNULL(b.orange_count, 0) + IFNULL(c.orange_count, 0)) AS orange_count
105+
FROM
106+
Boxes b
107+
LEFT JOIN
108+
Chests c
109+
ON
110+
b.chest_id = c.chest_id;
100111
```
101112

102113
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
SUM(IFNULL(b.apple_count, 0) + IFNULL(c.apple_count, 0)) AS apple_count,
4+
SUM(IFNULL(b.orange_count, 0) + IFNULL(c.orange_count, 0)) AS orange_count
5+
FROM
6+
Boxes b
7+
LEFT JOIN
8+
Chests c
9+
ON
10+
b.chest_id = c.chest_id;

0 commit comments

Comments
 (0)