小白刷力扣(最长连续序列-128)

题目描述:

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

示例:

输入:nums = [100,4,200,1,3,2]
输出:4

基础知识:

mp[t]++            为map中的value值(且设置value值为int)

for(auto t:nums)     遍历数组nums,将每一个字符元素分别赋予t,即对数组内的每个字符中的每个元素进行遍历

if/while(map[t-1])       为判断map[t-1]这个值是否存在,存在才继续

代码运行:

#include<unordered_map>
using namespace std;
class Solution{
public:
         int longestconsecutive(vector<int>&nums){
              unordered_map<int,int>mp;
              for(auto t:nums){
                mp[t]++;
           }
              int res=0;
              for(auto t:nums){
                  if(mp[t-1])contine;
                  int len=1;
                  while(mp[++t])
                  len++;
                  res=max(res,len);//取每一次的最大序列
          }
  
            return res;
 }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值