力扣面试题17.18.最短超串

力扣面试题17.18.最短超串

  • 类似76.

  • 用哈希表处理短数组 然后遍历长数组

    • 找到相同元素 count-- --
    • 当count==0时进入循环 ------ 尽可能缩小区间
cpp 复制代码
  class Solution {
  public:
      vector<int> shortestSeq(vector<int>& big, vector<int>& small) {
          int n=big.size(),m=small.size();
          int st=-1,ed=n;
          unordered_map<int,int> cnt;
          int count=0;  //记录small中元素个数
          for(auto c:small)
          {
              if(!cnt.count(c)) count++;
              cnt[c] ++;
          }
          for(int i=0,j=0;i<n;i++)
          {
              cnt[big[i]] --;
              if(cnt[big[i]] == 0) count--;
              while(!count)
              {
                  cnt[big[j]] ++;
                  //说明当前左端点为small中元素 并且big的区间内已不包含
                  if(cnt[big[j]] >0)
                  {
                      count++;
                      if(ed - st > i - j) st = j,ed = i;
                  }
                  j ++;
              }
          }
          if(st == -1) return {};
          else return {st,ed};
      }
  };
相关推荐
橘颂TA13 小时前
【剑斩OFFER】算法的暴力美学——库存管理 III
算法·力扣
Hcoco_me13 小时前
大模型面试题16:SVM 算法详解及实践
算法·数据挖掘·聚类
wearegogog1231 天前
光谱分析波段选择的连续投影算法
算法
执笔论英雄1 天前
【RL】DAPO 数据处理
算法
测试19981 天前
功能测试、自动化测试、性能测试的区别
自动化测试·python·功能测试·测试工具·职场和发展·性能测试·安全性测试
why1511 天前
面经整理——算法
java·数据结构·算法
悦悦子a啊1 天前
将学生管理系统改造为C/S模式 - 开发过程报告
java·开发语言·算法
痕忆丶1 天前
双线性插值缩放算法详解
算法
我命由我123451 天前
开发中的英语积累 P19:Inspect、Hint、Feedback、Direction、Compact、Vulnerability
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法