leetcode128 最长连续序列

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

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

python 复制代码
# -*- 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哈希表完整代码:

python 复制代码
# -*- 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++哈希表不完整代码:

python 复制代码
// 定义一个名为 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;//更新最长连续序列的长度
    }
};
python 复制代码
// 定义一个名为 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++哈希表完整代码:

python 复制代码
// -*- 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;
}
python 复制代码
// -*- 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,表示程序正常结束
}
python 复制代码
#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哈希表不完整代码:

python 复制代码
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; // 返回最长连续序列的长度
    }
}
python 复制代码
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哈希表完整代码:

python 复制代码
/**
 * @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); // 输出最长连续序列的长度
    }
}
python 复制代码
/**
 * @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); // 输出最长连续序列的长度
    }
}
相关推荐
学传打活5 天前
【边打字.边学昆仑正义文化】_进阶篇5 宇宙星系的运转(续1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
fangzhanpeng16812 天前
快速掌握C#语言基础知识点(1.基础数据类型)
开发语言·c#·1024程序员节
无己心15 天前
面向多平台 AI 搜索引擎的适配层架构设计
前端·人工智能·搜索引擎·ai·1024程序员节
u01030552720 天前
AI驱动的安卓UI自动生成功能实现
人工智能·1024程序员节
u01030552721 天前
长株潭智能体赋能腾讯元器链接共享
1024程序员节
云贝贝贝22 天前
OceanBase认证体系介绍与备考指南
运维·服务器·oceanbase·1024程序员节
u01030552722 天前
Java图像处理实战指南
人工智能·1024程序员节
u01030552723 天前
Java AWT鼠标事件全解析
人工智能·1024程序员节
u01030552725 天前
Java实用类应用精讲
人工智能·1024程序员节
u0103055271 个月前
Java I/O核心操作实战
人工智能·1024程序员节