双指针算法

例题1:最长连续不重复子序列

给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。

输入格式

第一行包含整数 n。

第二行包含 n个整数(均在 0∼105 范围内),表示整数序列。

输出格式

共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。

数据范围

1≤n≤ 1 0 5 10^5 105

输入样例:
5
1 2 2 3 5
输出样例:
3
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++)
            a[i] = Integer.parseInt(str[i]);
        int[] c = new int[100001];  //b【】作为桶记录每个数字出现的次数
        int res = 0;
        for (int i=0,j=0;i<n;i++){  //双指针
            c[a[i]]++;  //指向一个数,就把对应数的次数+1
            while (c[a[i]]>1){  //当次数>1时,说明当前区间有重复元素,就是a[i]
                c[a[j]]--;  //把j对应位置的数删掉
                j++;
            }
            res = Math.max(res, i-j+1);     //每次取不包含重复的数的连续区间的长度的最大值
        }
        System.out.println(res);
    }
}

例题2: 数组元素的目标和

给定两个升序排序的有序数组 A 和 B,以及一个目标值 x。

数组下标从 0开始。

请你求出满足 A[i]+B[j]=x的数对 (i,j)。

数据保证有唯一解。

输入格式

第一行包含三个整数 n,m,x分别表示 A的长度,B的长度以及目标值 x。

第二行包含 n个整数,表示数组 A。

第三行包含 m个整数,表示数组 B。

输出格式

共一行,包含两个整数 i 和 j。

数据范围

数组长度不超过 1 0 5 10^5 105
同一数组内元素各不相同。
1≤数组元素≤ 1 0 9 10^9 109

输入样例:
4 5 6
1 2 4 7
3 4 6 8 9
输出样例:
1 1
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        long x = Long.parseLong(str[2]);
        int[] a = new int[n];
        int[] b = new int[m];
        str = bufferedReader.readLine().split(" ");
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(str[i]);
        str = bufferedReader.readLine().split(" ");
        for (int i = 0; i < m; i++)
            b[i] = Integer.parseInt(str[i]);
        for (int i = 0, j = m - 1; i < n; i++) {
            if (a[i] < x) {
                while (j >= 0 && a[i] + b[j] > x) {
                    j--;
                }
                if (j >= 0 && a[i] + b[j] == x) {
                    System.out.println(i + " " + j);
                    break;
                }
            }
            j = m - 1;
        }
    }
}

例题3:判断子序列

给定一个长度为 n 的整数序列 a1,a2,…,an以及一个长度为 m的整数序列 b1,b2,…,bm。

请你判断 a序列是否为 b 序列的子序列。

子序列指序列的一部分项按原有次序排列而得的序列,例如序列 {a1,a3,a5}是序列 {a1,a2,a3,a4,a5}的一个子序列。

输入格式

第一行包含两个整数 n,m。

第二行包含 n个整数,表示 a1,a2,…,an。

第三行包含 m个整数,表示 b1,b2,…,bm。

输出格式

如果 a 序列是 b序列的子序列,输出一行 Yes

否则,输出 No

数据范围

1≤n≤m≤ 1 0 5 10^5 105,
1 0 9 10^9 109≤ai,bi≤ 1 0 9 10^9 109

输入样例:
3 5
1 3 5
1 2 3 4 5
输出样例:
Yes
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        int[] a = new int[n];
        int[] b = new int[m];
        str = bufferedReader.readLine().split(" ");
        for (int i = 0; i < n; i++)
            a[i] = Integer.parseInt(str[i]);
        str = bufferedReader.readLine().split(" ");
        for (int i = 0; i < m; i++)
            b[i] = Integer.parseInt(str[i]);
        int i=0, j=0;
        while (i<n&&j<m){
            if(a[i]==b[j])
                i++;
            j++;
        }
        if(i==n)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

I'm 程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值