Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-05-18 00:18:24
https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/description/
2130. Maximum Twin Sum of a Linked List
给你一个偶数数量的链结串行,返回最大的一对串行和,一对串行为串行对半切之后从
中心点分别往左和右有相同距离的Node。
Example 1:
https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png
Input: head = [5,4,2,1]
Output: 6
Explanation:
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum
= 6.
There are no other nodes with twins in the linked list.
Thus, the maximum twin sum of the linked list is 6.
Example 2:
https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png
Input: head = [1,100000]
Output: 100001
Explanation:
There is only one node with a twin in the linked list having twin sum of 1 +
100000 = 100001.
思路:
1.一个指标每次走一步,一个每次走两步,当快指标走完的时候慢指标会在中间,把
慢指标停留的值纪录在一个List。
2.慢指标继续一步一步往后走并和 List 的尾端元素开始往前相加,并更新最大值。
Java Code:
作者: jimmy888 (jimmy888)   2023-05-18 00:21:00
看到大家都会写程式 我又想那个了

Links booklink

Contact Us: admin [ a t ] ucptt.com