【java】浅谈双指针

双指针

思路及其实现

双指针就是在一个循环里有两个变量同时向前推进,循环结束条件可以是一个或多个(只要不是死循环就可)。一般模型为i(第一个指针,以下同),j(第二个指针,以下同)。第一种模型为一个数组(i从首位开始,j从末尾开始)第二种模型为二个数组(i从第一个数组首位开始,j从第二个数组尾部出发),第三钟为两个数组(i从第一个数组首位开始,j从第二个数组首位开始)

双指针一般都是两层循环优化而来,特别注意的是看我们不要的是否有用,才能确定是否使用双指针。

最长连续不重复子序列

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

输入格式

第一行包含整数 n。

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

输出格式

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

数据范围

1≤n≤105

输入样例:
5
1 2 2 3 5
输出样例:
3
//第一类模型
import java.io.*;

public class Main {
    static int N = 100010;
    static int[] a = new int[N];
    static int[] s = new int[N];
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        String[] s1 = in.readLine().split(" ");
        for(int i = 0; i < n ; i ++) a[i] = Integer.parseInt(s1[i]);
        int res = 0;
        for (int i = 0, j = 0; i < n; i ++) {
            s[a[i]] ++;
            //当容器中数字大于二(也就是出现重复数字),需要筛掉
            while(s[a[i]] > 1) {
                s[a[j]] --;
                j ++;
            }
            res = Math.max(res, i - j + 1);
        }
        System.out.println(res);
    } 
}

数组元素的目标和

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

数组下标从 0 开始。

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

数据保证有唯一解。

输入格式

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

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

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

输出格式

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

数据范围

数组长度不超过 105。
同一数组内元素各不相同。
1≤数组元素≤109

输入样例:
4 5 6
1 2 4 7
3 4 6 8 9
输出样例:
1 1
//第二类模板
import java.io.*;

public class Main {
    static int N = 100010;
    static int[] a = new int[N];
    static int[] b = new int[N];
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = in.readLine().split(" ");
        int n = Integer.parseInt(s1[0]);
        int m = Integer.parseInt(s1[1]);
        int x = Integer.parseInt(s1[2]);
        String[] s2 = in.readLine().split(" ");
        for(int i = 0; i < n ; i ++) a[i] = Integer.parseInt(s2[i]);
        String[] s3 = in.readLine().split(" ");
        for(int i = 0; i < m ; i ++) b[i] = Integer.parseInt(s3[i]);
        int i = 0;
        int j = m - 1;
        while(i < n && j > 0) {
            if(a[i] + b[j] > x) j --;
            else if(a[i] + b[j] < x) i ++;
            else {
                System.out.print(i + " " + j);
                //虽然只有一个,但是不加就tle了;
                break;
            }
        }
    } 
}

判断子序列

给定一个长度为 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
//第三类模板
import java.io.*;

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值