蓝桥杯JAVA-31.双指针、离散化、合并区间

个人博客
www.tothefor.com
蓝桥杯复习知识点汇总

目录

双指针

应用

1.统计单词数量

将Vector换成Set即可实现统计不同单词的数量。

package acmtest;

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

public class Main {
	public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		String str ;
		Vector<String> vStrings = new Vector<String>();
		str = sc.nextLine();
		int len = str.length();
		for(int i=0;i<len;++i) {
			int j = i;
			while(j<len&&str.charAt(j)!=' ') j++;
			vStrings.add(str.substring(i, j));
			i=j; //j当前处于空格处,所有还需要将i进行加1
		}
		cout.println(vStrings.size());
		cout.flush();
		
		closeAll();
		
	}
	
	public static int nextInt() throws Exception{
        cin.nextToken();
        return (int) cin.nval;
    }
    public static long nextLong() throws Exception{
        cin.nextToken();
        return (long) cin.nval;
    }
    public static double nextDouble() throws Exception{
        cin.nextToken();
        return cin.nval;
    }
    public static String nextString() throws Exception{
        cin.nextToken();
        return cin.sval;
    }
    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }
}

输入输出:

adsf wefaf fewaaef as fsd fwe far gdzsrwea ea
9
2.最长连续不重复子序列

题目链接1
题目链接2

连续是位置连续!

package acmtest;

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

public class Main {
	public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		String s1;
		s1=sc.next();
		int len = s1.length();
		int ans = 0;
		int[] flag = new int[100010];
		for(int i=0,j=0;i<len;++i) {
			flag[(int)s1.charAt(i)]++;
			while(flag[(int)s1.charAt(i)]>1) {
				flag[(int)s1.charAt(j)]--;
				j++;
			}
			ans=Math.max(ans, i-j+1);
		}
		cout.println(ans);
		cout.flush();
		
		closeAll();
		
	}
	
	public static int nextInt() throws Exception{
        cin.nextToken();
        return (int) cin.nval;
    }
    public static long nextLong() throws Exception{
        cin.nextToken();
        return (long) cin.nval;
    }
    public static double nextDouble() throws Exception{
        cin.nextToken();
        return cin.nval;
    }
    public static String nextString() throws Exception{
        cin.nextToken();
        return cin.sval;
    }
    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }
}

输入输出:

122356
4

数据离散化

离散化的步骤:

  • 1、排序

  • 2、去重

  • 3、索引

合并区间

  • 首先把数组排序,先按照起点升序排列,如果起点位置相同,按照结尾升序排序。

  • 设置start 和 end 为当前区间的开始和结尾。

  • 遍历数组,如果下一个的起点小于当前区间的结尾,那么就说明这两个区间有重叠,要进行合并,所以要更新结尾。

  • 如果下一个的起点,大于当前区间的结尾,说明这两个区间没有重叠。之前start,end是一个单独的区间,所以保存起来。保存之后,重新设置strat和end,继续向后进行合并。

import java.io.*;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @墨水记忆 www.tothefor.com
 */
public class Main {
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);

    //    cin.ordinaryChars('0', '9') ;
    //    cin.wordChars('0', '9');
    public static void main(String[] args) throws Exception {

        int[][] a = new int[][]{{1, 3}, {2, 6}, {8, 10}, {14, 17}, {12, 15}};
        Arrays.sort(a, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0]);
        List<int[]> res = new ArrayList<>();
        int start = a[0][0];
        int end = a[0][1];
        for (int i = 1; i < a.length; ++i) {
            if (a[i][0] <= end) { 
                end = Math.max(a[i][1], end);
            } else {
                res.add(new int[]{start, end});
                start = a[i][0];
                end = a[i][1];
            }
        }
        res.add(new int[]{start, end});
        int[][] ans = new int[res.size()][2];
        System.out.println("单独区间个数为:" + res.size());
        for (int i = 0; i < res.size(); ++i) {
            ans[i] = res.get(i);
        }
        System.out.println("区间分别是:");
        for (int i = 0; i < ans.length; ++i) {
            System.out.println(ans[i][0] + " " + ans[i][1]);
        }
        closeAll();
    }

    public static int nextInt() throws Exception {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static long nextLong() throws Exception {
        cin.nextToken();
        return (long) cin.nval;
    }

    public static double nextDouble() throws Exception {
        cin.nextToken();
        return cin.nval;
    }

    public static String nextString() throws Exception {
        cin.nextToken();
        return cin.sval;
    }

    public static void closeAll() throws Exception {
        cout.close();
        in.close();
        out.close();
    }

}

输入输出:

单独区间个数为:3
区间分别是:
1 6
8 10
12 17
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值