题解 | #数组中的最长连续子序列#

数组中的最长连续子序列

https://www.nowcoder.com/practice/eac1c953170243338f941959146ac4bf

class Solution {
public:
    /**
     * max increasing subsequence
     * @param arr int整型vector the array
     * @return int整型
     */
    int MLS(vector<int>& arr) {
        sort(arr.begin(), arr.end());
        int nowlen = 1, now = arr[0];
        int res = 1;
        for (int i = 1; i < arr.size(); ++i) {
            if (arr[i] == now + 1) {
                ++nowlen;
                now = arr[i];
                res = max(res, nowlen);
            } else if (arr[i] != now) {
                nowlen = 1;
                now = arr[i];
            }
        }
        return res;
    }
};

思路:先排序,然后模拟构造最长连续子序列。

全部评论

相关推荐

ohs的小木屋:比不少实习待遇高了
点赞 评论 收藏
分享
asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务