Skip to content

Commit 16db48b

Browse files
committed
feat: add solutions to lc problems: No.1047, 2130
- No.1047.Remove All Adjacent Duplicates In String - No.2130.Maximum Twin Sum of a Linked List
1 parent 2c67256 commit 16db48b

File tree

8 files changed

+376
-1
lines changed

8 files changed

+376
-1
lines changed

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,44 @@ var removeDuplicates = function (s) {
136136
};
137137
```
138138

139+
### **Rust**
140+
141+
```rust
142+
impl Solution {
143+
pub fn remove_duplicates(s: String) -> String {
144+
let mut stack = Vec::new();
145+
for c in s.chars() {
146+
if !stack.is_empty() && *stack.last().unwrap() == c {
147+
stack.pop();
148+
} else {
149+
stack.push(c);
150+
}
151+
}
152+
stack.into_iter().collect()
153+
}
154+
}
155+
```
156+
157+
### **C**
158+
159+
```c
160+
char *removeDuplicates(char *s) {
161+
int n = strlen(s);
162+
char *stack = malloc(sizeof(char) * (n + 1));
163+
int i = 0;
164+
for (int j = 0; j < n; j++) {
165+
char c = s[j];
166+
if (i && stack[i - 1] == c) {
167+
i--;
168+
} else {
169+
stack[i++] = c;
170+
}
171+
}
172+
stack[i] = '\0';
173+
return stack;
174+
}
175+
```
176+
139177
### **...**
140178
141179
```

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,44 @@ var removeDuplicates = function (s) {
126126
};
127127
```
128128

129+
### **Rust**
130+
131+
```rust
132+
impl Solution {
133+
pub fn remove_duplicates(s: String) -> String {
134+
let mut stack = Vec::new();
135+
for c in s.chars() {
136+
if !stack.is_empty() && *stack.last().unwrap() == c {
137+
stack.pop();
138+
} else {
139+
stack.push(c);
140+
}
141+
}
142+
stack.into_iter().collect()
143+
}
144+
}
145+
```
146+
147+
### **C**
148+
149+
```c
150+
char *removeDuplicates(char *s) {
151+
int n = strlen(s);
152+
char *stack = malloc(sizeof(char) * (n + 1));
153+
int i = 0;
154+
for (int j = 0; j < n; j++) {
155+
char c = s[j];
156+
if (i && stack[i - 1] == c) {
157+
i--;
158+
} else {
159+
stack[i++] = c;
160+
}
161+
}
162+
stack[i] = '\0';
163+
return stack;
164+
}
165+
```
166+
129167
### **...**
130168
131169
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
char *removeDuplicates(char *s) {
2+
int n = strlen(s);
3+
char *stack = malloc(sizeof(char) * (n + 1));
4+
int i = 0;
5+
for (int j = 0; j < n; j++) {
6+
char c = s[j];
7+
if (i && stack[i - 1] == c) {
8+
i--;
9+
} else {
10+
stack[i++] = c;
11+
}
12+
}
13+
stack[i] = '\0';
14+
return stack;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn remove_duplicates(s: String) -> String {
3+
let mut stack = Vec::new();
4+
for c in s.chars() {
5+
if !stack.is_empty() && *stack.last().unwrap() == c {
6+
stack.pop();
7+
} else {
8+
stack.push(c);
9+
}
10+
}
11+
stack.into_iter().collect()
12+
}
13+
}

solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md

+101-1
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,110 @@ func max(a, b int) int {
352352

353353
### **TypeScript**
354354

355-
<!-- 这里可写当前语言的特殊实现逻辑 -->
355+
```ts
356+
/**
357+
* Definition for singly-linked list.
358+
* class ListNode {
359+
* val: number
360+
* next: ListNode | null
361+
* constructor(val?: number, next?: ListNode | null) {
362+
* this.val = (val===undefined ? 0 : val)
363+
* this.next = (next===undefined ? null : next)
364+
* }
365+
* }
366+
*/
367+
368+
function pairSum(head: ListNode | null): number {
369+
const arr = [];
370+
let node = head;
371+
while (node) {
372+
arr.push(node.val);
373+
node = node.next;
374+
}
375+
const n = arr.length;
376+
let ans = 0;
377+
for (let i = 0; i < n >> 1; i++) {
378+
ans = Math.max(ans, arr[i] + arr[n - 1 - i]);
379+
}
380+
return ans;
381+
}
382+
```
356383

357384
```ts
385+
/**
386+
* Definition for singly-linked list.
387+
* class ListNode {
388+
* val: number
389+
* next: ListNode | null
390+
* constructor(val?: number, next?: ListNode | null) {
391+
* this.val = (val===undefined ? 0 : val)
392+
* this.next = (next===undefined ? null : next)
393+
* }
394+
* }
395+
*/
396+
397+
function pairSum(head: ListNode | null): number {
398+
let fast = head;
399+
let slow = head;
400+
while (fast) {
401+
fast = fast.next.next;
402+
slow = slow.next;
403+
}
404+
let prev = null;
405+
while (slow) {
406+
const next = slow.next;
407+
slow.next = prev;
408+
prev = slow;
409+
slow = next;
410+
}
411+
let left = head;
412+
let right = prev;
413+
let ans = 0;
414+
while (left && right) {
415+
ans = Math.max(ans, left.val + right.val);
416+
left = left.next;
417+
right = right.next;
418+
}
419+
return ans;
420+
}
421+
```
358422

423+
### **Rust**
424+
425+
```rust
426+
// Definition for singly-linked list.
427+
// #[derive(PartialEq, Eq, Clone, Debug)]
428+
// pub struct ListNode {
429+
// pub val: i32,
430+
// pub next: Option<Box<ListNode>>
431+
// }
432+
//
433+
// impl ListNode {
434+
// #[inline]
435+
// fn new(val: i32) -> Self {
436+
// ListNode {
437+
// next: None,
438+
// val
439+
// }
440+
// }
441+
// }
442+
impl Solution {
443+
pub fn pair_sum(head: Option<Box<ListNode>>) -> i32 {
444+
let mut arr = Vec::new();
445+
let mut node = &head;
446+
while node.is_some() {
447+
let t = node.as_ref().unwrap();
448+
arr.push(t.val);
449+
node = &t.next;
450+
}
451+
let n = arr.len();
452+
let mut ans = 0;
453+
for i in 0..n >> 1 {
454+
ans = ans.max(arr[i] + arr[n - 1 - i]);
455+
}
456+
ans
457+
}
458+
}
359459
```
360460

361461
### **...**

solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README_EN.md

+102
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,109 @@ func max(a, b int) int {
332332
### **TypeScript**
333333

334334
```ts
335+
/**
336+
* Definition for singly-linked list.
337+
* class ListNode {
338+
* val: number
339+
* next: ListNode | null
340+
* constructor(val?: number, next?: ListNode | null) {
341+
* this.val = (val===undefined ? 0 : val)
342+
* this.next = (next===undefined ? null : next)
343+
* }
344+
* }
345+
*/
335346

347+
function pairSum(head: ListNode | null): number {
348+
const arr = [];
349+
let node = head;
350+
while (node) {
351+
arr.push(node.val);
352+
node = node.next;
353+
}
354+
const n = arr.length;
355+
let ans = 0;
356+
for (let i = 0; i < n >> 1; i++) {
357+
ans = Math.max(ans, arr[i] + arr[n - 1 - i]);
358+
}
359+
return ans;
360+
}
361+
```
362+
363+
```ts
364+
/**
365+
* Definition for singly-linked list.
366+
* class ListNode {
367+
* val: number
368+
* next: ListNode | null
369+
* constructor(val?: number, next?: ListNode | null) {
370+
* this.val = (val===undefined ? 0 : val)
371+
* this.next = (next===undefined ? null : next)
372+
* }
373+
* }
374+
*/
375+
376+
function pairSum(head: ListNode | null): number {
377+
let fast = head;
378+
let slow = head;
379+
while (fast) {
380+
fast = fast.next.next;
381+
slow = slow.next;
382+
}
383+
let prev = null;
384+
while (slow) {
385+
const next = slow.next;
386+
slow.next = prev;
387+
prev = slow;
388+
slow = next;
389+
}
390+
let left = head;
391+
let right = prev;
392+
let ans = 0;
393+
while (left && right) {
394+
ans = Math.max(ans, left.val + right.val);
395+
left = left.next;
396+
right = right.next;
397+
}
398+
return ans;
399+
}
400+
```
401+
402+
### **Rust**
403+
404+
```rust
405+
// Definition for singly-linked list.
406+
// #[derive(PartialEq, Eq, Clone, Debug)]
407+
// pub struct ListNode {
408+
// pub val: i32,
409+
// pub next: Option<Box<ListNode>>
410+
// }
411+
//
412+
// impl ListNode {
413+
// #[inline]
414+
// fn new(val: i32) -> Self {
415+
// ListNode {
416+
// next: None,
417+
// val
418+
// }
419+
// }
420+
// }
421+
impl Solution {
422+
pub fn pair_sum(head: Option<Box<ListNode>>) -> i32 {
423+
let mut arr = Vec::new();
424+
let mut node = &head;
425+
while node.is_some() {
426+
let t = node.as_ref().unwrap();
427+
arr.push(t.val);
428+
node = &t.next;
429+
}
430+
let n = arr.len();
431+
let mut ans = 0;
432+
for i in 0..n >> 1 {
433+
ans = ans.max(arr[i] + arr[n - 1 - i]);
434+
}
435+
ans
436+
}
437+
}
336438
```
337439

338440
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 pair_sum(head: Option<Box<ListNode>>) -> i32 {
19+
let mut arr = Vec::new();
20+
let mut node = &head;
21+
while node.is_some() {
22+
let t = node.as_ref().unwrap();
23+
arr.push(t.val);
24+
node = &t.next;
25+
}
26+
let n = arr.len();
27+
let mut ans = 0;
28+
for i in 0..n >> 1 {
29+
ans = ans.max(arr[i] + arr[n - 1 - i]);
30+
}
31+
ans
32+
}
33+
}

0 commit comments

Comments
 (0)