摘要: 请用栈实现一个队列,支持如下四种操作: push(x) – 将元素x插到队尾; pop() – 将队首的元素弹出,并返回该元素; peek() – 返回队首元素; empty() – 返回队列是否为空; class MyQueue { public: /** Initialize your data 阅读全文
posted @ 2022-12-10 16:52 !&&|| 阅读(28) 评论(0) 推荐(0)
摘要: 给定一棵二叉树的其中一个节点,请找出中序遍历序列的下一个节点。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNod 阅读全文
posted @ 2022-12-10 16:42 !&&|| 阅读(26) 评论(0) 推荐(0)
摘要: 输入一棵二叉树前序遍历和中序遍历的结果,请重建该二叉树。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(i 阅读全文
posted @ 2022-12-10 16:21 !&&|| 阅读(20) 评论(0) 推荐(0)
摘要: 输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值。 返回的结果用数组存储。 class Solution { public: vector<int> printListReversingly(ListNode* head) { vector<int> ans; for (auto p = h 阅读全文
posted @ 2022-12-10 15:34 !&&|| 阅读(19) 评论(0) 推荐(0)
摘要: 请实现一个函数,把字符串中的每个空格替换成"%20"。 class Solution { public: string replaceSpaces(string &s) { string ans = ""; for (char c : s) { if (c == ' ') { ans += "%20 阅读全文
posted @ 2022-12-10 15:31 !&&|| 阅读(49) 评论(0) 推荐(0)
摘要: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 class Solution { public: bool searchArray(vector<vector<int>> arr 阅读全文
posted @ 2022-12-10 14:02 !&&|| 阅读(20) 评论(0) 推荐(0)
摘要: 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 注意:不能修改输入的数组。 class Solution { public: int duplicateInAr 阅读全文
posted @ 2022-12-10 13:59 !&&|| 阅读(27) 评论(0) 推荐(0)
摘要: 输入一个长度为 n 的整数数列,从小到大输出前 m 小的数。 #include <iostream> using namespace std; const int N = 1e5 + 10; int n, m, len; int h[N]; void down (int u) { int t = u 阅读全文
posted @ 2022-12-02 19:55 !&&|| 阅读(49) 评论(0) 推荐(0)
摘要: 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。 A 吃 B,B 吃 C,C 吃 A。 现有 N 个动物,以 1∼N 编号。 每个动物都是 A,B,C 中的一种,但是我们并不知道它到底是哪一种。 有人用两种说法对这 N个动物所构成的食物链关系进行描述: 第一种说法是 1 X Y 阅读全文
posted @ 2022-12-02 15:48 !&&|| 阅读(192) 评论(0) 推荐(0)
摘要: 给定一个数组。 有一个大小为k 的滑动窗口,它从数组的最左边移动到最右边。 你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。 #include <iostream> #include <deque> using namespace std; const int N = 1e6 + 10 阅读全文
posted @ 2022-11-30 22:02 !&&|| 阅读(34) 评论(0) 推荐(0)