Skip to content

Commit 487a5fc

Browse files
committed
feat: add solutions to lc problem: No.2130
No.2130.Maximum Twin Sum of a Linked List
1 parent 8098c8d commit 487a5fc

File tree

8 files changed

+261
-18
lines changed

8 files changed

+261
-18
lines changed

basic/sorting/BubbleSort/BubbleSort.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class Program
44
{
55
public static void Main()
66
{
7-
int[] test = new int[] {56,876,34,23,45,501,2,3,4,6,5,7,8,9,11,10,12,23,34};
7+
int[] test = new int[] { 56, 876, 34, 23, 45, 501, 2, 3, 4, 6, 5, 7, 8, 9, 11, 10, 12, 23, 34 };
88
BubbleSortNums(test);
99
foreach (var item in test)
1010
{
@@ -18,20 +18,20 @@ public static void BubbleSortNums(int[] nums)
1818
for (int initial = 0; initial < nums.Length - numchange; initial++)
1919
{
2020
WriteLine($"{initial} start ");
21-
//记录此值 用于迭代开始位置
21+
// 记录此值 用于迭代开始位置
2222
bool changelog = false;
2323
for (int second_sortnum = initial; second_sortnum < nums.Length - 1; second_sortnum++)
2424
{
2525
if (nums[second_sortnum] > nums[second_sortnum + 1])
2626
{
27-
swap(ref nums[second_sortnum], ref nums[second_sortnum+ 1]);
27+
swap(ref nums[second_sortnum], ref nums[second_sortnum + 1]);
2828
if (!changelog)
2929
{
30-
//记录转换的位置,让initial开始位置从转换位置前开始
31-
initial = ((second_sortnum - 2 )> 0) ? (second_sortnum - 2) : -1;
32-
numchange += 1;
30+
// 记录转换的位置,让initial开始位置从转换位置前开始
31+
initial = ((second_sortnum - 2) > 0) ? (second_sortnum - 2) : -1;
32+
numchange += 1;
3333
}
34-
changelog = true;
34+
changelog = true;
3535
}
3636
}
3737
}

basic/sorting/BubbleSort/README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,15 @@ fn main() {
189189
```
190190

191191
### **C#**
192+
192193
```cs
193194
using static System.Console;
194195
namespace Pro;
195196
public class Program
196197
{
197198
public static void Main()
198199
{
199-
int[] test = new int[] {56,876,34,23,45,501,2,3,4,6,5,7,8,9,11,10,12,23,34};
200+
int[] test = new int[] { 56, 876, 34, 23, 45, 501, 2, 3, 4, 6, 5, 7, 8, 9, 11, 10, 12, 23, 34 };
200201
BubbleSortNums(test);
201202
foreach (var item in test)
202203
{
@@ -210,20 +211,20 @@ public class Program
210211
for (int initial = 0; initial < nums.Length - numchange; initial++)
211212
{
212213
WriteLine($"{initial} start ");
213-
//记录此值 用于迭代开始位置
214+
// 记录此值 用于迭代开始位置
214215
bool changelog = false;
215216
for (int second_sortnum = initial; second_sortnum < nums.Length - 1; second_sortnum++)
216217
{
217218
if (nums[second_sortnum] > nums[second_sortnum + 1])
218219
{
219-
swap(ref nums[second_sortnum], ref nums[second_sortnum+ 1]);
220+
swap(ref nums[second_sortnum], ref nums[second_sortnum + 1]);
220221
if (!changelog)
221222
{
222-
//记录转换的位置,让initial开始位置从转换位置前开始
223-
initial = ((second_sortnum - 2 )> 0) ? (second_sortnum - 2) : -1;
224-
numchange += 1;
223+
// 记录转换的位置,让initial开始位置从转换位置前开始
224+
initial = ((second_sortnum - 2) > 0) ? (second_sortnum - 2) : -1;
225+
numchange += 1;
225226
}
226-
changelog = true;
227+
changelog = true;
227228
}
228229
}
229230
}
@@ -235,8 +236,6 @@ public class Program
235236
compare_right = temp;
236237
}
237238
}
238-
239-
240239
```
241240

242241
<!-- tabs:end -->

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

+85-1
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,99 @@
7373
<!-- 这里可写当前语言的特殊实现逻辑 -->
7474

7575
```python
76-
76+
# Definition for singly-linked list.
77+
# class ListNode:
78+
# def __init__(self, val=0, next=None):
79+
# self.val = val
80+
# self.next = next
81+
class Solution:
82+
def pairSum(self, head: Optional[ListNode]) -> int:
83+
s = []
84+
while head:
85+
s.append(head.val)
86+
head = head.next
87+
n = len(s)
88+
return max(s[i] + s[-(i + 1)] for i in range(n >> 1))
7789
```
7890

7991
### **Java**
8092

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

8395
```java
96+
/**
97+
* Definition for singly-linked list.
98+
* public class ListNode {
99+
* int val;
100+
* ListNode next;
101+
* ListNode() {}
102+
* ListNode(int val) { this.val = val; }
103+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
104+
* }
105+
*/
106+
class Solution {
107+
public int pairSum(ListNode head) {
108+
List<Integer> s = new ArrayList<>();
109+
for (; head != null; head = head.next) {
110+
s.add(head.val);
111+
}
112+
int ans = 0, n = s.size();
113+
for (int i = 0; i < (n >> 1); ++i) {
114+
ans = Math.max(ans, s.get(i) + s.get(n - 1 - i));
115+
}
116+
return ans;
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for singly-linked list.
126+
* struct ListNode {
127+
* int val;
128+
* ListNode *next;
129+
* ListNode() : val(0), next(nullptr) {}
130+
* ListNode(int x) : val(x), next(nullptr) {}
131+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
132+
* };
133+
*/
134+
class Solution {
135+
public:
136+
int pairSum(ListNode* head) {
137+
vector<int> s;
138+
for (; head != nullptr; head = head->next) s.push_back(head->val);
139+
int ans = 0, n = s.size();
140+
for (int i = 0; i < (n >> 1); ++i) ans = max(ans, s[i] + s[n - i - 1]);
141+
return ans;
142+
}
143+
};
144+
```
84145
146+
### **Go**
147+
148+
```go
149+
/**
150+
* Definition for singly-linked list.
151+
* type ListNode struct {
152+
* Val int
153+
* Next *ListNode
154+
* }
155+
*/
156+
func pairSum(head *ListNode) int {
157+
var s []int
158+
for ; head != nil; head = head.Next {
159+
s = append(s, head.Val)
160+
}
161+
ans, n := 0, len(s)
162+
for i := 0; i < (n >> 1); i++ {
163+
if ans < s[i]+s[n-i-1] {
164+
ans = s[i] + s[n-i-1]
165+
}
166+
}
167+
return ans
168+
}
85169
```
86170

87171
### **TypeScript**

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

+85-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,97 @@ There is only one node with a twin in the linked list having twin sum of 1 + 100
6262
### **Python3**
6363

6464
```python
65-
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 pairSum(self, head: Optional[ListNode]) -> int:
72+
s = []
73+
while head:
74+
s.append(head.val)
75+
head = head.next
76+
n = len(s)
77+
return max(s[i] + s[-(i + 1)] for i in range(n >> 1))
6678
```
6779

6880
### **Java**
6981

7082
```java
83+
/**
84+
* Definition for singly-linked list.
85+
* public class ListNode {
86+
* int val;
87+
* ListNode next;
88+
* ListNode() {}
89+
* ListNode(int val) { this.val = val; }
90+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
91+
* }
92+
*/
93+
class Solution {
94+
public int pairSum(ListNode head) {
95+
List<Integer> s = new ArrayList<>();
96+
for (; head != null; head = head.next) {
97+
s.add(head.val);
98+
}
99+
int ans = 0, n = s.size();
100+
for (int i = 0; i < (n >> 1); ++i) {
101+
ans = Math.max(ans, s.get(i) + s.get(n - 1 - i));
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
/**
112+
* Definition for singly-linked list.
113+
* struct ListNode {
114+
* int val;
115+
* ListNode *next;
116+
* ListNode() : val(0), next(nullptr) {}
117+
* ListNode(int x) : val(x), next(nullptr) {}
118+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
119+
* };
120+
*/
121+
class Solution {
122+
public:
123+
int pairSum(ListNode* head) {
124+
vector<int> s;
125+
for (; head != nullptr; head = head->next) s.push_back(head->val);
126+
int ans = 0, n = s.size();
127+
for (int i = 0; i < (n >> 1); ++i) ans = max(ans, s[i] + s[n - i - 1]);
128+
return ans;
129+
}
130+
};
131+
```
71132
133+
### **Go**
134+
135+
```go
136+
/**
137+
* Definition for singly-linked list.
138+
* type ListNode struct {
139+
* Val int
140+
* Next *ListNode
141+
* }
142+
*/
143+
func pairSum(head *ListNode) int {
144+
var s []int
145+
for ; head != nil; head = head.Next {
146+
s = append(s, head.Val)
147+
}
148+
ans, n := 0, len(s)
149+
for i := 0; i < (n >> 1); i++ {
150+
if ans < s[i]+s[n-i-1] {
151+
ans = s[i] + s[n-i-1]
152+
}
153+
}
154+
return ans
155+
}
72156
```
73157

74158
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
int pairSum(ListNode* head) {
14+
vector<int> s;
15+
for (; head != nullptr; head = head->next) s.push_back(head->val);
16+
int ans = 0, n = s.size();
17+
for (int i = 0; i < (n >> 1); ++i) ans = max(ans, s[i] + s[n - i - 1]);
18+
return ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func pairSum(head *ListNode) int {
9+
var s []int
10+
for ; head != nil; head = head.Next {
11+
s = append(s, head.Val)
12+
}
13+
ans, n := 0, len(s)
14+
for i := 0; i < (n >> 1); i++ {
15+
if ans < s[i]+s[n-i-1] {
16+
ans = s[i] + s[n-i-1]
17+
}
18+
}
19+
return ans
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public int pairSum(ListNode head) {
13+
List<Integer> s = new ArrayList<>();
14+
for (; head != null; head = head.next) {
15+
s.add(head.val);
16+
}
17+
int ans = 0, n = s.size();
18+
for (int i = 0; i < (n >> 1); ++i) {
19+
ans = Math.max(ans, s.get(i) + s.get(n - 1 - i));
20+
}
21+
return ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def pairSum(self, head: Optional[ListNode]) -> int:
8+
s = []
9+
while head:
10+
s.append(head.val)
11+
head = head.next
12+
n = len(s)
13+
return max(s[i] + s[-(i + 1)] for i in range(n >> 1))

0 commit comments

Comments
 (0)