Skip to content

Commit 16f059e

Browse files
committed
feat: add solutions to lc problems: No.0061,0433
- No.0061.Rotate List - No.0433.Minimum Genetic Mutation
1 parent f5addba commit 16f059e

File tree

7 files changed

+444
-0
lines changed

7 files changed

+444
-0
lines changed

solution/0000-0099/0061.Rotate List/README.md

+98
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,51 @@ function rotateRight(head: ListNode | null, k: number): ListNode | null {
165165
}
166166
```
167167

168+
```ts
169+
/**
170+
* Definition for singly-linked list.
171+
* class ListNode {
172+
* val: number
173+
* next: ListNode | null
174+
* constructor(val?: number, next?: ListNode | null) {
175+
* this.val = (val===undefined ? 0 : val)
176+
* this.next = (next===undefined ? null : next)
177+
* }
178+
* }
179+
*/
180+
181+
function rotateRight(head: ListNode | null, k: number): ListNode | null {
182+
if (head == null || k === 0) {
183+
return head;
184+
}
185+
186+
let n = 0;
187+
let cur = head;
188+
while (cur != null) {
189+
cur = cur.next;
190+
n++;
191+
}
192+
k = k % n;
193+
if (k === 0) {
194+
return head;
195+
}
196+
197+
cur = head;
198+
for (let i = 0; i < n - k - 1; i++) {
199+
cur = cur.next;
200+
}
201+
202+
const res = cur.next;
203+
cur.next = null;
204+
cur = res;
205+
while (cur.next != null) {
206+
cur = cur.next;
207+
}
208+
cur.next = head;
209+
return res;
210+
}
211+
```
212+
168213
### **C++**
169214

170215
```cpp
@@ -258,6 +303,59 @@ public class Solution {
258303
}
259304
```
260305

306+
### **Rust**
307+
308+
```rust
309+
// Definition for singly-linked list.
310+
// #[derive(PartialEq, Eq, Clone, Debug)]
311+
// pub struct ListNode {
312+
// pub val: i32,
313+
// pub next: Option<Box<ListNode>>
314+
// }
315+
//
316+
// impl ListNode {
317+
// #[inline]
318+
// fn new(val: i32) -> Self {
319+
// ListNode {
320+
// next: None,
321+
// val
322+
// }
323+
// }
324+
// }
325+
impl Solution {
326+
pub fn rotate_right(mut head: Option<Box<ListNode>>, mut k: i32) -> Option<Box<ListNode>> {
327+
if head.is_none() || k == 0 {
328+
return head;
329+
}
330+
let n = {
331+
let mut cur = &head;
332+
let mut res = 0;
333+
while cur.is_some() {
334+
cur = &cur.as_ref().unwrap().next;
335+
res += 1;
336+
}
337+
res
338+
};
339+
k = k % n;
340+
if k == 0 {
341+
return head;
342+
}
343+
344+
let mut cur = &mut head;
345+
for _ in 0..n - k - 1 {
346+
cur = &mut cur.as_mut().unwrap().next;
347+
}
348+
let mut res = cur.as_mut().unwrap().next.take();
349+
cur = &mut res;
350+
while cur.is_some() {
351+
cur = &mut cur.as_mut().unwrap().next;
352+
}
353+
*cur = head.take();
354+
res
355+
}
356+
}
357+
```
358+
261359
### **...**
262360

263361
```

solution/0000-0099/0061.Rotate List/README_EN.md

+98
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,51 @@ function rotateRight(head: ListNode | null, k: number): ListNode | null {
151151
}
152152
```
153153

154+
```ts
155+
/**
156+
* Definition for singly-linked list.
157+
* class ListNode {
158+
* val: number
159+
* next: ListNode | null
160+
* constructor(val?: number, next?: ListNode | null) {
161+
* this.val = (val===undefined ? 0 : val)
162+
* this.next = (next===undefined ? null : next)
163+
* }
164+
* }
165+
*/
166+
167+
function rotateRight(head: ListNode | null, k: number): ListNode | null {
168+
if (head == null || k === 0) {
169+
return head;
170+
}
171+
172+
let n = 0;
173+
let cur = head;
174+
while (cur != null) {
175+
cur = cur.next;
176+
n++;
177+
}
178+
k = k % n;
179+
if (k === 0) {
180+
return head;
181+
}
182+
183+
cur = head;
184+
for (let i = 0; i < n - k - 1; i++) {
185+
cur = cur.next;
186+
}
187+
188+
const res = cur.next;
189+
cur.next = null;
190+
cur = res;
191+
while (cur.next != null) {
192+
cur = cur.next;
193+
}
194+
cur.next = head;
195+
return res;
196+
}
197+
```
198+
154199
### **C++**
155200

156201
```cpp
@@ -244,6 +289,59 @@ public class Solution {
244289
}
245290
```
246291

292+
### **Rust**
293+
294+
```rust
295+
// Definition for singly-linked list.
296+
// #[derive(PartialEq, Eq, Clone, Debug)]
297+
// pub struct ListNode {
298+
// pub val: i32,
299+
// pub next: Option<Box<ListNode>>
300+
// }
301+
//
302+
// impl ListNode {
303+
// #[inline]
304+
// fn new(val: i32) -> Self {
305+
// ListNode {
306+
// next: None,
307+
// val
308+
// }
309+
// }
310+
// }
311+
impl Solution {
312+
pub fn rotate_right(mut head: Option<Box<ListNode>>, mut k: i32) -> Option<Box<ListNode>> {
313+
if head.is_none() || k == 0 {
314+
return head;
315+
}
316+
let n = {
317+
let mut cur = &head;
318+
let mut res = 0;
319+
while cur.is_some() {
320+
cur = &cur.as_ref().unwrap().next;
321+
res += 1;
322+
}
323+
res
324+
};
325+
k = k % n;
326+
if k == 0 {
327+
return head;
328+
}
329+
330+
let mut cur = &mut head;
331+
for _ in 0..n - k - 1 {
332+
cur = &mut cur.as_mut().unwrap().next;
333+
}
334+
let mut res = cur.as_mut().unwrap().next.take();
335+
cur = &mut res;
336+
while cur.is_some() {
337+
cur = &mut cur.as_mut().unwrap().next;
338+
}
339+
*cur = head.take();
340+
res
341+
}
342+
}
343+
```
344+
247345
### **...**
248346

249347
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
impl Solution {
18+
pub fn rotate_right(mut head: Option<Box<ListNode>>, mut k: i32) -> Option<Box<ListNode>> {
19+
if head.is_none() || k == 0 {
20+
return head;
21+
}
22+
let n = {
23+
let mut cur = &head;
24+
let mut res = 0;
25+
while cur.is_some() {
26+
cur = &cur.as_ref().unwrap().next;
27+
res += 1;
28+
}
29+
res
30+
};
31+
k = k % n;
32+
if k == 0 {
33+
return head;
34+
}
35+
36+
let mut cur = &mut head;
37+
for _ in 0..n - k - 1 {
38+
cur = &mut cur.as_mut().unwrap().next;
39+
}
40+
let mut res = cur.as_mut().unwrap().next.take();
41+
cur = &mut res;
42+
while cur.is_some() {
43+
cur = &mut cur.as_mut().unwrap().next;
44+
}
45+
*cur = head.take();
46+
res
47+
}
48+
}

solution/0400-0499/0433.Minimum Genetic Mutation/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,76 @@ func minMutation(start string, end string, bank []string) int {
344344
}
345345
```
346346

347+
### **TypeScript**
348+
349+
```ts
350+
function minMutation(start: string, end: string, bank: string[]): number {
351+
const queue = [start];
352+
let res = 0;
353+
while (queue.length !== 0) {
354+
const n = queue.length;
355+
for (let i = 0; i < n; i++) {
356+
const s1 = queue.shift();
357+
if (s1 === end) {
358+
return res;
359+
}
360+
361+
for (let j = bank.length - 1; j >= 0; j--) {
362+
const s2 = bank[j];
363+
let count = 0;
364+
for (let k = 0; k < 8; k++) {
365+
if (s1[k] !== s2[k]) {
366+
count++;
367+
}
368+
}
369+
if (count <= 1) {
370+
queue.push(...bank.splice(j, 1));
371+
}
372+
}
373+
}
374+
res++;
375+
}
376+
return -1;
377+
}
378+
```
379+
380+
### **Rust**
381+
382+
```rust
383+
use std::collections::VecDeque;
384+
impl Solution {
385+
pub fn min_mutation(start: String, end: String, mut bank: Vec<String>) -> i32 {
386+
let mut queue = vec![start].into_iter().collect::<VecDeque<String>>();
387+
let mut res = 0;
388+
while !queue.is_empty() {
389+
let n = queue.len();
390+
for _ in 0..n {
391+
let s1 = queue.pop_front().unwrap();
392+
if s1 == end {
393+
return res;
394+
}
395+
396+
for i in (0..bank.len()).rev() {
397+
let s1 = s1.as_bytes();
398+
let s2 = bank[i].as_bytes();
399+
let mut count = 0;
400+
for j in 0..8 {
401+
if s1[j] != s2[j] {
402+
count += 1;
403+
}
404+
}
405+
if count <= 1 {
406+
queue.push_back(bank.remove(i));
407+
}
408+
}
409+
}
410+
res += 1;
411+
}
412+
-1
413+
}
414+
}
415+
```
416+
347417
### **...**
348418

349419
```

0 commit comments

Comments
 (0)