leetcode128 最长连续序列

 🚀🏆🍀👇👇👇🌴🎄🎈🚀🔥🔥🔥🌟🍀🎉🎉🎉🎉

python3哈希表不完整代码:🌟🌟🍀🎉🎉🎉

# -*- coding: utf-8 -*-
# @Time    : 2023/9/11 22:10
# @Author  : 长沙有肥鱼
# @FileName: 最长连续序列.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/weixin_53660567?spm=1010.2135.3001.5343

class Solution:
    def longestConsecutive(self, nums):
        longest_streak = 0  # 定义最长连续序列的长度
        num_set = set(nums)  # 将所有数字放入一个集合中,去重
        for num in num_set:  # 如果当前的数是一个连续的的起点,统计这个连续序列的长度
            if num - 1 not in num_set:  # 如果当前数减去1不在连续序列中
                current_num = num
                current_streak = 1  # 连续序列的长度为1
                while(current_num + 1) in num_set:  # 如果当前数+1在连续序列中
                    current_num += 1  # 不断查找连续序列,直到num的下一个数组不存在于数组中
                    current_streak += 1
                longest_streak = max(longest_streak, current_streak)  # 更新最长连续序列的长度

        return longest_streak  # 返回连续最长序列的大小

python3哈希表完整代码: 

# -*- coding: utf-8 -*-
# @Time    : 2023/9/11 22:10
# @Author  : 长沙有肥鱼
# @FileName: 最长连续序列.py
# @Software: PyCharm
# @Blog    :https://blog.csdn.net/weixin_53660567?spm=1010.2135.3001.5343

class Solution:
    def longestConsecutive(self, nums):
        longest_streak = 0  # 定义最长连续序列的长度
        num_set = set(nums)  # 将所有数字放入一个集合中,去重
        for num in num_set:  # 如果当前的数是一个连续的的起点,统计这个连续序列的长度
            if num - 1 not in num_set:  # 如果当前数减去1不在连续序列中
                current_num = num
                current_streak = 1  # 连续序列的长度为1
                while(current_num + 1) in num_set:  # 如果当前数+1在连续序列中
                    current_num += 1  # 不断查找连续序列,直到num的下一个数组不存在于数组中
                    current_streak += 1
                longest_streak = max(longest_streak, current_streak)  # 更新最长连续序列的长度

        return longest_streak  # 返回连续最长序列的大小


# 示例用法
nums = [100, 4, 200, 1, 3, 2]
solution = Solution()  # 实例化Solution类
result = solution.longestConsecutive(nums)  # 调用twoSum函数
print("最长连续序列长度为:", result)

c++哈希表不完整代码:

// 定义一个名为 Solution 的类
class Solution{
public:
    int longestConsecutive(vector<int> &nums){
        int longest_streak = 0;//定义最长连续序列的长度
        // 将所有数字放入一个无序集合中去重
        unordered_set<int> num_set(nums.begin(),nums.end());
        //如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for(int num : nums){
            //如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (num_set.find(num - 1) == num_set.end()){
                int current_num = num;
                int current_streak = 1;
                while(num_set.find(current_num + 1) != num_set.end()){
                    current_num += 1; //当前数+1
                    current_streak += 1; //当前连续序列大小+1
                }
                longest_streak = max(longest_streak, current_streak);//更新最长连续序列的长度
            }
        }
        return longest_streak;//更新最长连续序列的长度
    }
};
// 定义一个名为 Solution 的类
class Solution{
public:
    int longestConsecutive(vector<int> &nums){
        int longest_streak = 0;//定义最长连续序列的长度
        // 将所有数字放入一个无序集合中去重
        unordered_set<int> num_set(nums.begin(),nums.end());
        //如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for(int num : nums){
            //如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (!num_set.count(num - 1)){
                int current_streak = 1;
                while(num_set.count(num++)){
                    num += 1; //当前数+1
                    longest_streak += 1; //当前连续序列大小+1
                }
                longest_streak = max(longest_streak, current_streak);//更新最长连续序列的长度
            }
        }
        return longest_streak;//更新最长连续序列的长度
    }
};

c++哈希表完整代码: 

// -*- coding: utf-8 -*-
// @Time    : 2023/9/11 22:45
// @Author  : 长沙有肥鱼
// @FileName: 最长连续序列.cpp
// @Software: CLion
// @Blog    : https://blog.csdn.net/weixin_53660567?spm=1010.2135.3001.5343

#include<iostream>// 输入输出流库
#include<vector> // 向量容器库
#include<algorithm>
#include<unordered_set>

using namespace std;
// 定义一个名为 Solution 的类
class Solution{
public:
    int longestConsecutive(vector<int> &nums){
        int longest_streak = 0;//定义最长连续序列的长度
        // 将所有数字放入一个无序集合中去重
        unordered_set<int> num_set(nums.begin(),nums.end());
        //如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for(int num : nums){
            //如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (num_set.find(num - 1) == num_set.end()){
                int current_num = num;
                int current_streak = 1;
                while(num_set.find(current_num + 1) != num_set.end()){
                    current_num += 1; //当前数+1
                    longest_streak += 1; //当前连续序列大小+1
                }
                longest_streak = max(longest_streak, current_streak);//更新最长连续序列的长度
            }
        }
        return longest_streak;//更新最长连续序列的长度
    }
};

int main(){
    std::vector<int> nums = {100, 4, 200, 1, 3, 2}; // 定义一个整数向量 nums
    Solution solution;
    int result = solution.longestConsecutive(nums);// 调用 longestConsecutive 函数,传入 nums
    cout<<"最长连续序列长度为:"<<result<<endl;// 输出最长连续序列的长度
    return 0;
}
// -*- coding: utf-8 -*-
// @Time    : 2023/9/11 22:45
// @Author  : 长沙有肥鱼
// @FileName: 最长连续序列.cpp
// @Software: CLion
// @Blog    : https://blog.csdn.net/weixin_53660567?spm=1010.2135.3001.5343

#include<iostream>// 输入输出流库
#include<vector> // 向量容器库
#include<algorithm> // 标准算法库
#include<unordered_set> // 无序集合库

using namespace std; // 使用标准命名空间

// 定义一个名为 Solution 的类
class Solution{
public:
    int longestConsecutive(vector<int> &nums){ // 定义一个函数,接收一个整数向量的引用作为参数
        int longest_streak = 0;//定义最长连续序列的长度

        // 将所有数字放入一个无序集合中去重
        unordered_set<int> num_set(nums.begin(),nums.end());

        //如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for(int num : nums){ // 对于向量中的每一个元素
            //如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (!num_set.count(num - 1)){ // 如果 num-1 不在 num_set 中
                int current_num = num; // 将当前数赋给 current_num
                int current_streak = 1; // 当前连续序列的长度为1
                while(num_set.count(current_num + 1)){ // 当 current_num + 1 在 num_set 中
                    current_num += 1; //当前数+1
                    current_streak += 1; //当前连续序列大小+1
                }
                longest_streak = max(longest_streak, current_streak); // 更新最长连续序列的长度
            }
        }
        return longest_streak; // 返回最长连续序列的长度
    }
};

int main(){ // 主函数
    std::vector<int> nums = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; // 定义一个整数向量 nums

    Solution solution; // 实例化Solution类
    int result = solution.longestConsecutive(nums); // 调用 longestConsecutive 函数,传入 nums
    cout<<"最长连续序列长度为:"<<result<<endl; // 输出最长连续序列的长度
    return 0; // 返回0,表示程序正常结束
}
#include <iostream> // 包含输入输出流库
#include <vector>   // 包含向量容器库
#include <unordered_set> // 包含无序集合库

using namespace std; // 使用标准命名空间

// 定义一个名为 Solution 的类
class Solution{
public:
    int longestConsecutive(vector<int>& nums) {// 定义一个函数,接收一个整数向量的引用作为参数
        unordered_set<int> numSet(nums.begin(), nums.end()); // 将所有数字放入一个无序集合中去重
        int longestStreak = 0;//定义最长连续序列的长度
        //如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for (int num : nums) {
            //如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (numSet.find(num - 1) == numSet.end()) {// 如果 num-1 不在 num_set 中
                int currentNum = num; // 将当前数赋给 current_num
                int currentStreak = 1;// 当前连续序列的长度为1

                // 继续查找连续序列中的下一个数字
                while (numSet.find(currentNum + 1) != numSet.end()) {// 当 current_num + 1 在 num_set 中
                    currentNum += 1;//当前数+1
                    currentStreak += 1;//当前连续序列大小+1
                }

                // 更新最长连续序列的长度
                longestStreak = max(longestStreak, currentStreak);
            }
        }

        return longestStreak;// 返回最长连续序列的长度
    }
};


int main(){ // 主函数
    std::vector<int> nums = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; // 定义一个整数向量 nums

    Solution solution; // 实例化Solution类
    int result = solution.longestConsecutive(nums); // 调用 longestConsecutive 函数,传入 nums
    cout<<"最长连续序列长度为:"<<result<<endl; // 输出最长连续序列的长度
    return 0; // 返回0,表示程序正常结束
}

 Java哈希表不完整代码:

class Solution {
    public static int longestConsecutive(int[] nums) { // 定义一个函数,接收一个整数数组作为参数
        HashSet<Integer> num_set = new HashSet<>(); // 创建一个HashSet,用于存储数字
        for (int num : nums) {
            num_set.add(num); // 将所有数字放入集合中去重
        }
        int longest_streak = 0; // 定义最长连续序列的长度

        // 如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for (int num : nums) { // 对于数组中的每一个元素
            // 如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (!num_set.contains(num - 1)) { // 如果 num-1 不在 num_set 中
                int current_num = num; // 将当前数赋给 current_num
                int current_streak = 1; // 当前连续序列的长度为1
                while (num_set.contains(current_num + 1)) { // 当 current_num 在 num_set 中
                    current_num += 1;//当签数+1
                    current_streak += 1; // 当前连续序列大小+1
                }
                longest_streak = Math.max(longest_streak, current_streak); // 更新最长连续序列的长度
            }
        }
        return longest_streak; // 返回最长连续序列的长度
    }
}
class Solution {
    public static int longestConsecutive(int[] nums) { // 定义一个函数,接收一个整数数组作为参数
        HashSet<Integer> num_set = new HashSet<>(); // 创建一个HashSet,用于存储数字
        for (int num : nums) {
            num_set.add(num); // 将所有数字放入集合中去重
        }
        int longest_streak = 0; // 定义最长连续序列的长度

        // 如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for (int num : nums) { // 对于数组中的每一个元素
            // 如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (!num_set.contains(num - 1)) { // 如果 num-1 不在 num_set 中
                int current_num = num; // 将当前数赋给 current_num
                int current_streak = 1; // 当前连续序列的长度为1
                while (num_set.contains(++current_num)) { // 当 current_num 在 num_set 中
                    current_streak += 1; // 当前连续序列大小+1
                }
                longest_streak = Math.max(longest_streak, current_streak); // 更新最长连续序列的长度
            }
        }
        return longest_streak; // 返回最长连续序列的长度
    }
}

Java哈希表完整代码:

/**
 * @Time : 2023/9/11 23:22
 * @Author : 长沙有肥鱼
 * @FileName: longestConsecutive.java
 * @Software: IntelliJ IDEA
 * @Blog :https://blog.csdn.net/weixin_53660567?spm=1010.2135.3001.5343 22
 **/

import java.util.HashSet;

public class longestConsecutive {
    public static int longestConsecutive(int[] nums) {
        HashSet<Integer> numSet = new HashSet<>(); // 创建一个哈希集合,用于存储数字
        for (int num : nums) {
            numSet.add(num); // 将所有数字放入集合中,去重
        }

        int longestStreak = 0;

        for (int num : nums) {
            if (!numSet.contains(num - 1)) { // 只考虑当前数字是一个连续序列的起点
                int currentNum = num;
                int currentStreak = 1;

                while (numSet.contains(currentNum + 1)) { // 继续查找连续序列中的下一个数字
                    currentNum += 1;
                    currentStreak += 1;
                }

                longestStreak = Math.max(longestStreak, currentStreak); // 更新最长连续序列的长度
            }
        }

        return longestStreak;
    }

    public static void main(String[] args) {
        int[] nums = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; // 定义一个整数数组 nums
        int result = longestConsecutive(nums); // 调用 longestConsecutive 函数,传入 nums

        System.out.println("最长连续序列长度为: " + result); // 输出最长连续序列的长度
    }
}
/**
 * @Time : 2023/9/11 23:22
 * @Author : 长沙有肥鱼
 * @FileName: longestConsecutive.java
 * @Software: IntelliJ IDEA
 * @Blog :https://blog.csdn.net/weixin_53660567?spm=1010.2135.3001.5343
 **/

import java.util.HashSet; // 导入HashSet类

public class longestConsecutive {
    public static int longestConsecutive(int[] nums) { // 定义一个函数,接收一个整数数组作为参数
        HashSet<Integer> num_set = new HashSet<>(); // 创建一个HashSet,用于存储数字
        for (int num : nums) {
            num_set.add(num); // 将所有数字放入集合中去重
        }
        int longest_streak = 0; // 定义最长连续序列的长度

        // 如果当前的数是一个连续序列的起点,统计这个连续序列的长度
        for (int num : nums) { // 对于数组中的每一个元素
            // 如果集合 num_set 中不存在 num - 1 这个元素,即当前数字 num 是一个连续序列的起点
            if (!num_set.contains(num - 1)) { // 如果 num-1 不在 num_set 中
                int current_num = num; // 将当前数赋给 current_num
                int current_streak = 1; // 当前连续序列的长度为1
                while (num_set.contains(++current_num)) { // 当 current_num 在 num_set 中
                    current_streak += 1; // 当前连续序列大小+1
                }
                longest_streak = Math.max(longest_streak, current_streak); // 更新最长连续序列的长度
            }
        }
        return longest_streak; // 返回最长连续序列的长度
    }

    public static void main(String[] args) {
        int[] nums = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; // 定义一个整数数组 nums
        int result = longestConsecutive(nums); // 调用 longestConsecutive 函数,传入 nums
        System.out.println("最长连续序列的长度为:" + result); // 输出最长连续序列的长度
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙有肥鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值