1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-24 06:18:57 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/最长斐波那契子数组(English) [longest-fibonacci-subarray].html
2025-10-19 23:12:56 +08:00

59 lines
2.1 KiB
HTML

<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>A <strong>Fibonacci</strong> array is a contiguous sequence whose third and subsequent terms each equal the sum of the two preceding terms.</p>
<p>Return the length of the longest <strong>Fibonacci</strong> <strong><span data-keyword="subarray-nonempty">subarray</span></strong> in <code>nums</code>.</p>
<p><strong>Note:</strong> Subarrays of length 1 or 2 are always <strong>Fibonacci</strong>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,2,3,5,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest Fibonacci subarray is <code>nums[2..6] = [1, 1, 2, 3, 5]</code>.</p>
<p><code>[1, 1, 2, 3, 5]</code> is Fibonacci because <code>1 + 1 = 2</code>, <code>1 + 2 = 3</code>, and <code>2 + 3 = 5</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,2,7,9,16]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest Fibonacci subarray is <code>nums[0..4] = [5, 2, 7, 9, 16]</code>.</p>
<p><code>[5, 2, 7, 9, 16]</code> is Fibonacci because <code>5 + 2 = 7</code>, <code>2 + 7 = 9</code>, and <code>7 + 9 = 16</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1000000000,1000000000,1000000000]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest Fibonacci subarray is <code>nums[1..2] = [1000000000, 1000000000]</code>.</p>
<p><code>[1000000000, 1000000000]</code> is Fibonacci because its length is 2.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>