Java 双指针算法

双指针

双指针在快速排序中和归并排序中,以及二分法中…都已经用到了,原理就是创建两个变量当做指向标点,然后通过单调性的规律来使两个指针移动,最终找到方案或答案。
例题来进一步了解双指针

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

输入格式
第一行包含整数n。

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

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

数据范围
1≤n≤100000
输入样例:
5
1 2 2 3 5
输出样例:
3

【题解:】
题目中求最长不重复区间,区间是由0-10^5组成的数字

1. 首先我们可以创建一个长度为100010长度的int数组,多10单位是因为怕越界
2. 然后用十万长度的数组下标来表示区间中每个数字,下标所对应的数字是出现的次数
3.定义两个指针,从区间初始开始,当期其中一个指针指向的数字在前面出现过,那么就移动另一个指针往前移动,移动之后,指针所指区间是无重复的,使用max取一个最大值

在这里插入图片描述

当i = 5 ,j = 3 时,b[] 数组里的每一个数都不大于1,所以这个区间是不重复的,i - j + 1 = 3,所以最大不重复区间就是3

【代码:】

import java.io.*;

public class Main {
    
    static int N = 100010;
    static int[] a = new int[N], b = new int[N];
    
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(read.readLine());
        String[] str = read.readLine().split(" ");
        for(int i = 0; i < n; i++) a[i + 1] = Integer.parseInt(str[i]);
        
        int res = 0;
        for (int i = 1, j = 1; i <= n; i++) {
            b[a[i]] ++;
            while(b[a[i]] > 1) {  //当数组中大于1,说明有重复的,就一直j++ ,使区间不存在重复数字
                b[a[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。

数据范围
数组长度不超过100000。
同一数组内元素各不相同。
1≤数组元素≤109
输入样例:
4 5 6
1 2 4 7
3 4 6 8 9
输出样例:
1 1

【思路:】
1.创建两个指针i,j,一个在数组a初始位置,一个在数组b末尾
2.如果a[i] + b[j] > x , 就 j - - ,因为都是递增数组,保证有解,那么解肯定在 j 左边,不可能在右边了
3.如果 当 a[i] + b[j] <= x 的时候,就判断是否==x , 如果等就输出结束,不相等就 i ++ ,因为是递增数组,那么 i 左边的数越来越小,更不可能等于x 了

【代码:】

import java.io.*;

public class Main {
    
    static int N = 100010;
    static int[] a = new int[N], b = new int[N]; 
    static int n, m, x;
    
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        String[] str1 = read.readLine().split(" ");
        n = Integer.parseInt(str1[0]);
        m = Integer.parseInt(str1[1]);
        x = Integer.parseInt(str1[2]);
        String[] str2 = read.readLine().split(" ");
        String[] str3 = read.readLine().split(" ");
        for(int i = 0; i < n; i++) a[i] = Integer.parseInt(str2[i]);
        for(int i = 0; i < m; i++) b[i] = Integer.parseInt(str3[i]);
        
        for(int i = 0, j = m - 1; i < n; i++) {
            while(j >= 0 && a[i] + b[j] > x) j--;
            if(a[i] + b[j] == x) {
                System.out.println(i + " " + j);
                return;
            }
        }
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

【例题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≤105,
−109≤ai,bi≤109
输入样例:
3 5
1 3 5
1 2 3 4 5
输出样例:
Yes

【思路:】
1.创建两个双指针 i , j 分别指向a,b两个数组的初始位置
2.如果b[j] == a[i] ,那么i++ ,j++
3.如果b[j] < a[i] ,那么只 j ++ , 最后如果i == a.length 那么就是子序列,否则不是

【代码:】

import java.io.*;

public class Main {
    
    static int N = 100010;
    static int[] a = new int[N], b = new int[N];
    static int n, m;
    
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        String[] str1 = read.readLine().split(" ");
        n = Integer.parseInt(str1[0]);
        m = Integer.parseInt(str1[1]);
        String[] str2 = read.readLine().split(" ");
        String[] str3 = read.readLine().split(" ");
        read.close();
        for(int i = 0; i < n; i++) a[i] = Integer.parseInt(str2[i]);
        for(int i = 0; i < m; i++) b[i] = Integer.parseInt(str3[i]);
        int i = 0, j = 0;
        while(j < m) {
            if(a[i] == b[j]) i++;
            j++;
        }
        if(i == n) System.out.println("Yes");
        else System.out.println("No");
    }
}

强烈推荐AcWing是一个非常不错的学习和刷题网站,课程价格也大幅低于市场上的课程,本博主也在用(https://www.acwing.com/problem/)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Oak Coffee

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

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

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

打赏作者

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

抵扣说明:

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

余额充值