当前位置:网站首页>2130. maximum twin sum of linked list
2130. maximum twin sum of linked list
2022-06-11 08:56:00 【Drag is me】
leetcode Force button to brush questions and punch in
* subject :2130. The largest twins in the linked list are
describe : In a size of n And n by even numbers In the linked list , about 0 <= i <= (n / 2) - 1 Of i , The first i Nodes ( Subscript from 0 Start ) The twin node of is (n-1-i) Nodes .
For example ,n = 4 Then the node 0 Is the node 3 Twin nodes of , node 1 Is the node 2 Twin nodes of . This is the length of n = 4 All twin nodes in the linked list .
Twin sum It is defined as the sum of the values of a node and its twin nodes .
Give you the head node of a linked list with an even length head , Please return to the linked list The largest twins and .
Their thinking
1、 The array stores the values of the linked list nodes ;
2、 Compare the maximum twins and ;
Source code ##
class Solution {
public:
int pairSum(ListNode* head) {
vector<int>v;
while (head) {
v.emplace_back(head->val);
head = head->next;
}
int sum = 0, ans = 0, n = v.size();
for (int i = 0; i < n / 2; ++i) {
sum = v[i] + v[n - 1 - i];
ans = max(ans, sum);
}
return ans;
}
};
边栏推荐
猜你喜欢

Textview text size auto fit and textview margin removal

c语言打印心形

Sword finger offer 51 Reverse pair in array
![[Clickhouse column] user initialization of new library role](/img/00/a11fb1a8e38ed4e0634839160c8ead.png)
[Clickhouse column] user initialization of new library role

M1 chip guide: M1, M1 pro, M1 Max and M1 ultra

处理RAW格式的图像,需要什么软件?

Matlab learning 7- linear smoothing filtering of image processing

【新手上路常见问答】关于数据可视化

Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波

C language printing heart
随机推荐
206. 反转链表
(1) AAC opening - lifecycle, livedata, ViewModel and source code analysis skills of core component principles (Reprint)
What if the copied code format is confused?
预编译,编译,汇编,链接,静态库,动态库都是什么
Getting started with Zipkin
Sword finger offer 51 Reverse pair in array
标准化编写知识
File system check of the root filesystem failed
c语言打印心形
M1 chip guide: M1, M1 pro, M1 Max and M1 ultra
C language printing diamond
445. 两数相加 II
Not eligible for getting processed by all beanpostprocessors
Sword finger offer 62 The last remaining number in the circle
203. 移除链表元素
2130. 链表最大孪生和
SQL basic query
EN 45545 R24氧指数测试方法解析
844. 比较含退格的字符串
EN 45545-2:2020 T11烟毒性检测