当前位置:网站首页>2130. 链表最大孪生和
2130. 链表最大孪生和
2022-06-11 08:55:00 【拽拽就是我】
leetcode力扣刷题打卡
*题目:2130. 链表最大孪生和
描述:在一个大小为 n 且 n 为 偶数 的链表中,对于 0 <= i <= (n / 2) - 1 的 i ,第 i 个节点(下标从 0 开始)的孪生节点为第 (n-1-i) 个节点 。
比方说,n = 4 那么节点 0 是节点 3 的孪生节点,节点 1 是节点 2 的孪生节点。这是长度为 n = 4 的链表中所有的孪生节点。
孪生和 定义为一个节点和它孪生节点两者值之和。
给你一个长度为偶数的链表的头节点 head ,请你返回链表的 最大孪生和 。
解题思路
1、数组存储链表节点值;
2、比较最大孪生和;
原代码##
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;
}
};
边栏推荐
- 剑指 Offer 10- II. 青蛙跳台阶问题
- js 中 Map 和 Set 的用法及区别
- Interprocess communication
- leetcode - 230. The k-th smallest element in a binary search tree
- What are the requirements of the drive housing UL 2043?
- Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
- [node] NPM part
- GCC AVR (ATMEL studio+ AVR studio) how to define the structure array in the program memory (flash) space and read it
- Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
- 补2:圆环回原点问题
猜你喜欢

端口占用问题,10000端口

Implementation of CRF for named entity recognition

What is the process of en 1101 flammability test for curtains?

CodeTop - 排序奇升偶降链表

leetcode - 518. Change II

Android 面试笔录(精心整理篇)

SAP OData development tutorial

命名实体识别之CRF的实现方式

Matlab学习9-图像处理之非线性锐化滤波

Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
随机推荐
Sword finger offer 62 The last remaining number in the circle
Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
Installation (detailed illustration) and use of SVN
木板ISO 5660-1 热量释放速率摸底测试
Android 面试笔录(精心整理篇)
Create a nodejs based background service using express+mysql
The interviewer asked four questions and summed up four experiences
Using docker compose to build redis5 cluster
Supplement 2: circle returning to origin
En45545-2 R26 vertical combustion test introduction
【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程
MATLAB R2022a 安装教程
Matlab learning 7- linear smoothing filtering of image processing
预编译,编译,汇编,链接,静态库,动态库都是什么
Which Apple devices support this system update? See if your old apple device supports the latest system
命名实体识别之CRF的实现方式
redis6 入门级教程,有整合案例,可以直接看整合案例,简单入门,直接上手
Heap can also be regarded as a tree structure. It is specified that the root node must be greater than or less than the left and right child nodes, but the size order of the left and right child nodes
Cron expressions in scheduled tasks
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?