首页 > 其他分享 >每日一题-双指针

每日一题-双指针

时间:2022-11-10 09:15:08浏览次数:39  
标签:do ++ 每日 two 一题 pointers 指针

判断子序列

int j = 0, i = 0;
	
while (i < m and j < n) {
    if (b[i] == a[j]) {
        j ++;
    }
    i ++;
}

cout << (j == n? "Yes" : "No");

description

for given sequence \(a\) and \(b\),decide whether \(a\) is a subsequence of \(b\).

two-pointers

for each \(a_i\), we greedily take the first occurence of it in \(b\). obviously, this strategy will mostly cover the elements in \(a\). To do this,
we use an \(O(n)\) algorithm called two pointers.
Pointer \(i\) and \(j\) point to \(a\) and \(b\) respectively. While \(i++\), if we find \(a_j == b_i\), then do \(j++\).

标签:do,++,每日,two,一题,pointers,指针
From: https://www.cnblogs.com/whose-dream/p/16875891.html

相关文章

  • 每日更新版
    目录10.25-html1、html5有哪些新特性?2、lframe的作用及其优缺点?3、常见浏览器内核是什么?4、image标签title与alt属性的区别是什么?5、对WEB标准以及W3C的理解与认识?6......
  • 每日一题之请描述Vue组件渲染流程
    组件化是Vue,React等这些框架的一个核心思想,通过把页面拆成一个个高内聚、低耦合的组件,可以极大程度提高我们的代码复用度,同时也使得项目更加易于维护。所以,本文就来分......
  • C++面经:C++多态-----虚函数、虚函数表、虚函数指针、虚继承
    1.虚函数引入类中之后,类会发生什么变化?首先我们创建一个空类A,然后创建一个类的对象a,并打印它的占用空间大小---为1   我们再往类中添加两个成员函数后,再返回对象......
  • 每日一题-区间分组
    区间分组 sort(a.begin(),a.end()); priority_queue<int,vector<int>,greater<int>>q; for(inti=0;i<n;++i){ if(q.empty()orq.top()>=a[i].fir......
  • 每日一题-区间覆盖
    区间覆盖sort(a.begin(),a.end()); intans=0,las=s; for(inti=0;i<n;++i){ intj=i; intr=-2e9; while(j<nanda[j].first<=......
  • 每日一题-叠罗汉的牛
    叠罗汉的牛 sort(a.begin(),a.end(),[](constauto&A,constauto&B){ returnA.first+A.second<B.first+B.second; }); intsum=0,ans=-2e9; ......
  • 指针
    空指针:空指针指向的内存编号为0;用于初始化指针变量(指针变量必须先初始化再使用(*p的操作))空指针指向的内存空间是不可以被访问的(不可以进行*p的操作)(内存空间为0~255是不可......
  • 函数指针和函数指针类型
    函数指针1.     定义每一个函数都占用一段内存单元,它们有一个起始地址,指向函数入口地址的指针称为函数指针。注意:函数指针的本质是一个指针变量,且指针指向的函数......
  • 用指针比较三个数大小(从小到大)
    #include<stdio.h>intswap(int*p1,int*p2){ inttemp; temp=*p1; *p1=*p2; *p2=temp; return0;}intmain(){ inta; intb; intc; int*pa; int*pb;......
  • 牛客java选择题每日打卡Day15
    牛客java选择题每日打卡Day15......