双端队列 码蹄集

题目来源:码蹄集

题目描述:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题意分析:

这道题目需要使用到双端队列的数据结构。我们可以借助 STL 中的 deque 来实现这个数据结构。具体来说,我们可以通过 deque 的 push_front 和 push_back 操作在队列的头部和尾部添加元素;通过 front 和 back 操作访问队列的头部和尾部元素;通过 pop_front 和 pop_back 操作删除队列的头部和尾部元素。

具体地,对于每个操作,我们可以按照题意依次执行相应的操作即可:

  1. 将整数 x 添加到队列的头部,可以使用 deque 的 push_front 操作。

  2. 将整数 x 添加到队列的尾部,可以使用 deque 的 push_back 操作。

  3. 访问队列的头部元素,可以使用 deque 的 front 操作。

  4. 访问队列的尾部元素,可以使用 deque 的 back 操作。

  5. 删除队列的头部元素,可以使用 deque 的 pop_front 操作。

  6. 删除队列的尾部元素,可以使用 deque 的 pop_back 操作。

最后,对于每个操作 3 和 4,我们需要输出一行表示答案的整数。

C++代码实现:

原文参考链接:https://betheme.net/dashuju/49396.html?action=onClick

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, op, x, length;
    deque<int> ideq;
    cin >> n;

    while (n--) {
        scanf("%d", &op);

        if (op == 1) {
            scanf("%d", &x);
            ideq.push_front(x);
        } else if (op == 2) {
            scanf("%d", &x);
            ideq.push_back(x);
        } else if (op == 3) {
            printf("%d\n", ideq.front());
        } else if (op == 4) {
            length = ideq.size();
            printf("%d\n", ideq.back());
        } else if (op == 5) {
            ideq.pop_front();
        } else {
            ideq.pop_back();
        }
    }

    return 0;
}

Python代码实现(会超时):

from collections import deque

n = int(input())
ideq = deque()

for i in range(n):
    op = list(map(int, input().split()))

    if op[0] == 1:
        ideq.appendleft(op[1])
    elif op[0] == 2:
        ideq.append(op[1])
    elif op[0] == 3:
        print(ideq[0])
    elif op[0] == 4:
        print(ideq[-1])
    elif op[0] == 5:
        ideq.popleft()
    else:
        ideq.pop()


Python代码实现(改进):

from collections import deque
import sys

n = int(sys.stdin.readline())
ideq = deque()

for i in range(n):
    op = list(map(int, sys.stdin.readline().split()))

    if op[0] == 1:
        ideq.appendleft(op[1])
    elif op[0] == 2:
        ideq.append(op[1])
    elif op[0] == 3:
        if len(ideq) > 0:
            print(ideq[0])
        else:
            print("error")
    elif op[0] == 4:
        if len(ideq) > 0:
            print(ideq[-1])
        else:
            print("error")
    elif op[0] == 5:
        if len(ideq) > 0:
            ideq.popleft()
    else:
        if len(ideq) > 0:
            ideq.pop()


Java代码实现(会超时):

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        Deque<Integer> ideq = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            int op = scan.nextInt();

            if (op == 1) {
                int x = scan.nextInt();
                ideq.offerFirst(x);
            } else if (op == 2) {
                int x = scan.nextInt();
                ideq.offerLast(x);
            } else if (op == 3) {
                System.out.println(ideq.peekFirst());
            } else if (op == 4) {
                System.out.println(ideq.peekLast());
            } else if (op == 5) {
                ideq.pollFirst();
            } else {
                ideq.pollLast();
            }
        }
    }
}

Java代码实现(改进):

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        Deque<Integer> ideq = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            String[] line = reader.readLine().split(" ");
            int op = Integer.parseInt(line[0]);

            if (op == 1) {
                int x = Integer.parseInt(line[1]);
                ideq.offerFirst(x);
            } else if (op == 2) {
                int x = Integer.parseInt(line[1]);
                ideq.offerLast(x);
            } else if (op == 3) {
                if (!ideq.isEmpty()) {
                    System.out.println(ideq.peekFirst());
                } else {
                    System.out.println("error");
                }
            } else if (op == 4) {
                if (!ideq.isEmpty()) {
                    System.out.println(ideq.peekLast());
                } else {
                    System.out.println("error");
                }
            } else if (op == 5) {
                if (!ideq.isEmpty()) {
                    ideq.pollFirst();
                }
            } else {
                if (!ideq.isEmpty()) {
                    ideq.pollLast();
                }
            }
        }
    }
}

代码提交测试结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Magneto_万磁王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值