成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 873. Length of Longest Fibonacci Subseq

haoguo / 585人閱讀

Problem

A sequence X_1, X_2, ..., X_n is fibonacci-like if:

n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.

(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)

Example 1:

Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:

Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].

Note:

3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.)

Solution
class Solution {
    public int lenLongestFibSubseq(int[] A) {
        Set set = new HashSet<>();
        for (int a: A) set.add(a);
        int max = 2;
        for (int i = 0; i < A.length-1; i++) {
            for (int j = i+1; j < A.length; j++) {
                int a1 = A[i], a2 = A[j];
                int curMax = 2;
                while (set.contains(a1+a2)) {
                    curMax++;
                    int temp = a1;
                    a1 = a2;
                    a2 = temp+a2;
                }
                max = Math.max(max, curMax);
            }
        }
        return max == 2 ? 0 : max;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72670.html

相關(guān)文章

  • Leetcode 3 Longest Substring Without Repeat... 最長無

    摘要:難度題意是求最長無重復(fù)子串給出一個(gè)字符串從所有子串中找出最長且沒有重復(fù)字母的子串的長度我的解法是以為例使用一個(gè)記錄當(dāng)前子串遇到的所有字符用一個(gè)游標(biāo)從頭開始讀取字符加入到中如果碰到了重復(fù)字符遇到了重復(fù)則從當(dāng)前子串的頭部的字符開始將該字符從中移 Longest Substring Without Repeating CharactersGiven a string, find the le...

    RyanHoo 評論0 收藏0
  • [LeetCode] 388. Longest Absolute File Path

    Problem Suppose we abstract our file system by a string in the following manner: The string dirntsubdir1ntsubdir2nttfile.ext represents: dir subdir1 subdir2 file.ext The directory dir ...

    wawor4827 評論0 收藏0
  • [Leetcode] Binary Tree Longest Consecutive Sequenc

    摘要:遞歸法復(fù)雜度時(shí)間空間思路因?yàn)橐易铋L的連續(xù)路徑,我們在遍歷樹的時(shí)候需要兩個(gè)信息,一是目前連起來的路徑有多長,二是目前路徑的上一個(gè)節(jié)點(diǎn)的值。代碼判斷當(dāng)前是否連續(xù)返回當(dāng)前長度,左子樹長度,和右子樹長度中較大的那個(gè) Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the lon...

    xi4oh4o 評論0 收藏0
  • LeetCode——Longest Substring Without Repeating Char

    摘要:原問題我的沙雕解法無重復(fù)字母存在重復(fù)字母挨打最暴力的無腦解法,耗時(shí)。。。 原問題 Given a string, find the length of the?longest substring?without repeating characters. Example 1: Input: abcabcbb Output: 3 Explanation: The answer is a...

    forsigner 評論0 收藏0
  • [Leetcode]Longest Substring Without Repeating Char

    摘要:解題思路本題借助實(shí)現(xiàn)。如果字符未出現(xiàn)過,則字符,如果字符出現(xiàn)過,則維護(hù)上次出現(xiàn)的遍歷的起始點(diǎn)。注意點(diǎn)每次都要更新字符的位置最后返回時(shí),一定要考慮到從到字符串末尾都沒有遇到重復(fù)字符的情況,所欲需要比較下和的大小。 Longest Substring Without Repeating CharactersGiven a string, find the length of the lon...

    awesome23 評論0 收藏0

發(fā)表評論

0條評論

haoguo

|高級講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<