AcWing 803. 区间合并

本文介绍了使用Java编程解决区间合并问题的解题思路,通过输入n个区间的左右端点,对左端点进行排序并合并重叠区间,计算最终不重叠区间的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解题思路
将所维护的区间和下一段区间进行比较。
将st ed设置成-2e9的目的是为了让第一个区间可以方便进入
相关代码

import java.util.*;

public class Main {
    public static void main(String[] args){
        List<PII> list = new ArrayList();
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for(int i=0;i<n;i++) {
            int l = scanner.nextInt();
            int r = scanner.nextInt();
            list.add(new PII(l,r));
        }
        //左端点进行排序
        Collections.sort(list, new Comparator<PII>() {
            @Override
            public int compare(PII o1, PII o2) {
                return o1.first-o2.first;
            }
        });

        int count = 0;   //记录有几个区间
        int st = (int) -2e9;
        int ed = (int)-2e9;

        for(PII p:list){
            if(p.first>ed){
                count++;
                st = p.first;
                ed = p.second;
            }
            else{
                ed = Math.max(ed,p.second);
            }
        }
        System.out.println(count);

    }
}

class PII {
    int first;
    int second;
    PII(int first,int second){
        this.first=first;
        this.second=second;
    }
}
### 关于双链表的实现 双链表是一种重要的数据结构,它由节点组成,每个节点包含三个部分:前驱指针、数据域和后继指针。这种结构允许双向遍历,因此相较于单链表更加灵活。 以下是双链表的核心操作实现: #### 节点定义 ```java class Node { int value; Node prev, next; public Node(int value) { this.value = value; this.prev = null; this.next = null; } } ``` #### 插入操作 在双链表中插入新节点的操作需要调整前后节点的关系。 ```java public void insert(Node head, int position, int newValue) { Node newNode = new Node(newValue); if (position == 0) { // 头插法 newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; } else { Node current = head; for (int i = 0; i < position - 1 && current != null; i++) { current = current.next; } if (current != null) { newNode.next = current.next; newNode.prev = current; if (current.next != null) { current.next.prev = newNode; } current.next = newNode; } } } ``` #### 删除操作 删除指定位置的节点时需要注意更新其前后节点的连接关系。 ```java public void delete(Node head, int position) { if (position == 0) { // 删除头结点 if (head != null) { head = head.next; if (head != null) { head.prev = null; } } } else { Node current = head; for (int i = 0; i < position && current != null; i++) { current = current.next; } if (current != null) { if (current.prev != null) { current.prev.next = current.next; } if (current.next != null) { current.next.prev = current.prev; } } } } ``` 以上代码展示了如何通过 Java 来实现双链表的基本功能[^1]。 --- ### AcWing 上的相关练习题 AcWing 提供了许多与双链表相关的经典题目,这些题目可以帮助巩固对双链表的理解并提升编程能力。以下是一些推荐的练习题: 1. **827. 双链表** 题目描述通常涉及构建一个支持增删查改操作的双链表,并测试其实现是否正确[^3]。 2. **模拟队列/栈** 使用双链表作为底层存储结构来实现队列或栈的功能,考察对其基本操作的应用。 3. **动态数组扩展** 结合双链表的特点设计一种能够自动扩容的数据容器,类似于 `ArrayList` 的行为。 4. **区间合并问题** 利用双链表解决某些区间的交集、并集运算等问题,这类场景下双链表的优势尤为明显。 上述题目均可以在 AcWing 数据结构章节找到,建议按照难度逐步尝试完成[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值