Skip to content

Commit f6c0eb8

Browse files
committed
feat: add solutions to lc problem: No.0817
No.0817.Linked List Components
1 parent ac631bf commit f6c0eb8

File tree

5 files changed

+224
-12
lines changed

5 files changed

+224
-12
lines changed

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

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,7 @@ func numComponents(head *ListNode, nums []int) int {
192192
* @return {number}
193193
*/
194194
var numComponents = function (head, nums) {
195-
const s = new Set();
196-
for (const v of nums) {
197-
s.add(v);
198-
}
195+
const s = new Set(nums);
199196
let ans = 0;
200197
while (head) {
201198
while (head && !s.has(head.val)) {
@@ -210,6 +207,83 @@ var numComponents = function (head, nums) {
210207
};
211208
```
212209

210+
### **TypeScript**
211+
212+
```ts
213+
/**
214+
* Definition for singly-linked list.
215+
* class ListNode {
216+
* val: number
217+
* next: ListNode | null
218+
* constructor(val?: number, next?: ListNode | null) {
219+
* this.val = (val===undefined ? 0 : val)
220+
* this.next = (next===undefined ? null : next)
221+
* }
222+
* }
223+
*/
224+
225+
function numComponents(head: ListNode | null, nums: number[]): number {
226+
const set = new Set<number>(nums);
227+
let res = 0;
228+
let cur = head;
229+
let inSet = false;
230+
while (cur != null) {
231+
if (set.has(cur.val)) {
232+
if (!inSet) {
233+
inSet = true;
234+
res++;
235+
}
236+
} else {
237+
inSet = false;
238+
}
239+
cur = cur.next;
240+
}
241+
return res;
242+
}
243+
```
244+
245+
### **Rust**
246+
247+
```rust
248+
// Definition for singly-linked list.
249+
// #[derive(PartialEq, Eq, Clone, Debug)]
250+
// pub struct ListNode {
251+
// pub val: i32,
252+
// pub next: Option<Box<ListNode>>
253+
// }
254+
//
255+
// impl ListNode {
256+
// #[inline]
257+
// fn new(val: i32) -> Self {
258+
// ListNode {
259+
// next: None,
260+
// val
261+
// }
262+
// }
263+
// }
264+
use std::collections::HashSet;
265+
impl Solution {
266+
pub fn num_components(head: Option<Box<ListNode>>, nums: Vec<i32>) -> i32 {
267+
let set = nums.into_iter().collect::<HashSet<i32>>();
268+
let mut res = 0;
269+
let mut in_set = false;
270+
let mut cur = &head;
271+
while let Some(node) = cur {
272+
if set.contains(&node.val) {
273+
if !in_set {
274+
in_set = true;
275+
res += 1;
276+
}
277+
} else {
278+
in_set = false;
279+
}
280+
cur = &node.next;
281+
}
282+
res
283+
}
284+
}
285+
```
286+
213287
### **...**
214288

215289
```

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

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ func numComponents(head *ListNode, nums []int) int {
172172
* @return {number}
173173
*/
174174
var numComponents = function (head, nums) {
175-
const s = new Set();
176-
for (const v of nums) {
177-
s.add(v);
178-
}
175+
const s = new Set(nums);
179176
let ans = 0;
180177
while (head) {
181178
while (head && !s.has(head.val)) {
@@ -190,6 +187,83 @@ var numComponents = function (head, nums) {
190187
};
191188
```
192189

190+
### **TypeScript**
191+
192+
```ts
193+
/**
194+
* Definition for singly-linked list.
195+
* class ListNode {
196+
* val: number
197+
* next: ListNode | null
198+
* constructor(val?: number, next?: ListNode | null) {
199+
* this.val = (val===undefined ? 0 : val)
200+
* this.next = (next===undefined ? null : next)
201+
* }
202+
* }
203+
*/
204+
205+
function numComponents(head: ListNode | null, nums: number[]): number {
206+
const set = new Set<number>(nums);
207+
let res = 0;
208+
let cur = head;
209+
let inSet = false;
210+
while (cur != null) {
211+
if (set.has(cur.val)) {
212+
if (!inSet) {
213+
inSet = true;
214+
res++;
215+
}
216+
} else {
217+
inSet = false;
218+
}
219+
cur = cur.next;
220+
}
221+
return res;
222+
}
223+
```
224+
225+
### **Rust**
226+
227+
```rust
228+
// Definition for singly-linked list.
229+
// #[derive(PartialEq, Eq, Clone, Debug)]
230+
// pub struct ListNode {
231+
// pub val: i32,
232+
// pub next: Option<Box<ListNode>>
233+
// }
234+
//
235+
// impl ListNode {
236+
// #[inline]
237+
// fn new(val: i32) -> Self {
238+
// ListNode {
239+
// next: None,
240+
// val
241+
// }
242+
// }
243+
// }
244+
use std::collections::HashSet;
245+
impl Solution {
246+
pub fn num_components(head: Option<Box<ListNode>>, nums: Vec<i32>) -> i32 {
247+
let set = nums.into_iter().collect::<HashSet<i32>>();
248+
let mut res = 0;
249+
let mut in_set = false;
250+
let mut cur = &head;
251+
while let Some(node) = cur {
252+
if set.contains(&node.val) {
253+
if !in_set {
254+
in_set = true;
255+
res += 1;
256+
}
257+
} else {
258+
in_set = false;
259+
}
260+
cur = &node.next;
261+
}
262+
res
263+
}
264+
}
265+
```
266+
193267
### **...**
194268

195269
```

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
* @return {number}
1212
*/
1313
var numComponents = function (head, nums) {
14-
const s = new Set();
15-
for (const v of nums) {
16-
s.add(v);
17-
}
14+
const s = new Set(nums);
1815
let ans = 0;
1916
while (head) {
2017
while (head && !s.has(head.val)) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
use std::collections::HashSet;
18+
impl Solution {
19+
pub fn num_components(head: Option<Box<ListNode>>, nums: Vec<i32>) -> i32 {
20+
let set = nums.into_iter().collect::<HashSet<i32>>();
21+
let mut res = 0;
22+
let mut in_set = false;
23+
let mut cur = &head;
24+
while let Some(node) = cur {
25+
if set.contains(&node.val) {
26+
if !in_set {
27+
in_set = true;
28+
res += 1;
29+
}
30+
} else {
31+
in_set = false;
32+
}
33+
cur = &node.next;
34+
}
35+
res
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function numComponents(head: ListNode | null, nums: number[]): number {
14+
const set = new Set<number>(nums);
15+
let res = 0;
16+
let cur = head;
17+
let inSet = false;
18+
while (cur != null) {
19+
if (set.has(cur.val)) {
20+
if (!inSet) {
21+
inSet = true;
22+
res++;
23+
}
24+
} else {
25+
inSet = false;
26+
}
27+
cur = cur.next;
28+
}
29+
return res;
30+
}

0 commit comments

Comments
 (0)