【C++】OJ习题 篇2

2024-08-31 16:28
文章标签 c++ 习题 oj

本文主要是介绍【C++】OJ习题 篇2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

头像
🚀个人主页:奋斗的小羊
🚀所属专栏:C++
很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎 ~

动图描述

目录

    • 💥1、删除有序数组中的重复项
    • 💥2、数组中出现次数超过一半的数字
    • 💥3、最小栈
    • 💥4、栈的压入、弹出序列
    • 💥5、环形链表
    • 💥6、环形链表 II
    • 💥7、用队列实现栈
    • 💥8、用栈实现队列
    • 💥9、设计循环队列


💥1、删除有序数组中的重复项

  • Leetcode——删除有序数组中的重复项

在这里插入图片描述
示例:

在这里插入图片描述

可以用快慢指针,快指针表示遍历数组到达的下标位置,慢指针表示下一个不同元素要填入的下标位置,初始时两个指针都指向下标1,这是为了体现慢指针记录不重复的数据个数。
删除重复项和找不重复的项效果是一样的。

class Solution {
public:int removeDuplicates(vector<int>& nums) {int slow = 1;for (int fast = 1; fast < nums.size(); ++fast){if (nums[fast] != nums[fast - 1]){nums[slow++] = nums[fast];}}return slow;}
};

💥2、数组中出现次数超过一半的数字

  • 牛客——数组中超过一半的数字

在这里插入图片描述

方法一:候选法

  • 时间复杂度:O(N)
  • 空间复杂度:O(1)

初始化一个候选目标val和得票数count,遍历数组,如果当前的得票数count为0的话就选当前在数组中拿到的元素为目标,如果得票数count不为0,有和val相等的元素就给它投一票,遇到不相等的就减一票。 遍历完数组后val就是出现次数超过数组长度一般的数。

  • 我们暂且将出现次数超过数组长度一半的数称作众数。数组中如果两个数不相等,就消去这两个数,最坏情况下,每次消去一个众数和一个非众数,那么如果存在众数,最后留下的数肯定是众数
class Solution {
public:int MoreThanHalfNum_Solution(vector<int>& numbers) {int val = 0;int count = 0;for (int e : numbers){if (0 == count){val = e;++count;}else {count = val == e ? ++count : --count;}}return val;}
};

方法二:排序法

  • 时间负责度:O(N*logN)
  • 空间负责度:O(1)

既然众数的个数超过了数组长度的一半,那有序数组中间位置的数一定就是众数。

class Solution {
public:int MoreThanHalfNum_Solution(vector<int>& numbers) {sort(numbers.begin(), numbers.end());return numbers[numbers.size() / 2];}
};

💥3、最小栈

  • Leetcode——最小栈

在这里插入图片描述
定义一个主栈和辅助栈,主栈支持push、pop、top操作,辅助栈用于存相比于栈顶数据更小的或相等的数,主栈pop时如果栈顶数据和辅助栈栈顶数据相等,辅助栈也跟着pop,那么常数时间内检索到的最小元素就是辅助栈栈顶数据。

在这里插入图片描述

class MinStack {
public:MinStack() {}void push(int val) {_st.push(val);if (_minst.empty() || val <= _minst.top()){_minst.push(val);}}void pop() {if (_st.top() == _minst.top()){_minst.pop();}_st.pop();}int top() {return _st.top();}int getMin() {return _minst.top();}
private:stack<int> _st;stack<int> _minst;
};

💥4、栈的压入、弹出序列

  • 牛客——栈的压入、弹出序列

在这里插入图片描述

定义一个栈用于压入数据,一个下标用于访问弹出序列。将压入序列依次放入栈中,期间如果某次压入的值和弹出序列的第一个数相等,那么就弹出刚压入的这个数,再++下标。
其中弹出栈中的数时要保证栈不为空,当访问完所有的压入数据后,检查栈是否为空,如果为空则返回真,否则返回假。

class Solution {
public:bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {// write code hereint i = 0;for (int e : pushV){_st.push(e);while (!_st.empty() && _st.top() == popV[i]){_st.pop();++i;}}return _st.empty();}
private:stack<int> _st;
};

💥5、环形链表

  • Leetcode——环形链表

在这里插入图片描述

快慢指针法: 快指针和慢指针初始时指向头节点,当快指针指向和快指针指向节点内的next指针不为空时,快指针一次走两步,慢指针一次走一步,快指针入环后走N圈后慢指针入环,当快指针和慢指针相等时说明存在环,如果出循环则说明不存在环。

关键的地方是快指针一次走两步,慢指针一次走一步,如果存在环则快指针和慢指针一定会相遇。为什么一定会相遇呢?
如果存在环,假设当慢指针入环时快指针距离此时慢指针的位置为N,则接下来每当快指针追赶慢指针一次,它们的距离就减一,直到减为0,此时快慢指针就相遇了。

在这里插入图片描述

bool hasCycle(struct ListNode *head) {struct ListNode* fast = head, *slow = head;while (fast && fast->next){fast = fast->next->next;slow = slow->next;if (fast == slow){return true;}}return false;
}

💥6、环形链表 II

  • Leetcode——环形链表 II

在这里插入图片描述

还是快慢指针,当快慢指针相遇时我们让meet指针指向相遇时的节点,然后让头指针headmeet指针一步步地向后走,当两指针相遇时指向的节点就是链表开始入环的第一个节点。为什么这两个指针一定会相遇在链表开始入环的第一个节点?

假设头指针距离链表开始入环的第一个节点的长度为L,meet指针相距链表开始入环的第一个节点的距离是N,环的长度为C,当慢指针入环时快指针走了x圈,因为快指针的速度是慢指针的2倍,那我们可以得到下面的等式:

  • 2(L + N) = L + X*C + N

化简得:L = X*C - N,由这个等式可以得出headmeet相遇是必然的。
在这里插入图片描述

struct ListNode *detectCycle(struct ListNode *head) {struct ListNode* fast = head, *slow = head;while (fast && fast->next){fast = fast->next->next;slow = slow->next;if (fast == slow){struct ListNode* meet = fast;while (head != meet){head = head->next;meet = meet->next;}return meet;}}return NULL;
}

💥7、用队列实现栈

  • Leetcode——用队列实现栈

在这里插入图片描述

栈的特点是后进先出,队列的特点是先进先出,用队列实现栈,必须有一个辅助队列在栈数据pop的时候用来导数据,将栈中需要pop的数据放到队列的队头pop
也就是说用队列实现栈需要两个队列,一个存数据一个导数据,一个为空一个不为空,其中入栈时往不为空的队列中入数据,为空的队列只有一个作用,就是栈pop数据时导数据。
其中队列还有一个重要的特点,就是出队列不会改变数据的相对位置。

在这里插入图片描述

typedef struct {Que q1;Que q2;
} MyStack;MyStack* myStackCreate() {MyStack* pst = (MyStack*)malloc(sizeof(MyStack));QueueInit(&pst->q1);QueueInit(&pst->q2);return pst;
}void myStackPush(MyStack* obj, int x) {if (QueueEmpty(&obj->q1)){QueuePush(&obj->q2, x);}else{QueuePush(&obj->q1, x);}
}int myStackPop(MyStack* obj) {//假设法Que* empty = &obj->q1;Que* noempty = &obj->q2;if (!QueueEmpty(&obj->q1)){empty = &obj->q2;noempty = &obj->q1;}while (QueueSize(noempty) > 1){QueuePush(empty, QueueFront(noempty));QueuePop(noempty);}int top = QueueFront(noempty);QueuePop(noempty);return top;
}int myStackTop(MyStack* obj) {if (!QueueEmpty(&obj->q1)){return QueueBack(&obj->q1);}else{return QueueBack(&obj->q2);}
}bool myStackEmpty(MyStack* obj) {return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}void myStackFree(MyStack* obj) {QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);
}

💥8、用栈实现队列

  • Leetcode——用栈实现队列

在这里插入图片描述

用两个栈实现队列,这里有两个方法。
方法一:和用两个队列实现栈类似,其中的一个栈用来导数据,因为栈的特点是后进先出,所以将栈中的数据导过来会让数据的相对位置颠倒,所以最后还需要将数据重新导回来才能保证数据的相对位置不变。

typedef int st_data_type;typedef struct stack
{st_data_type* arr;int top;int capacity;
}stack;
void stack_init(stack* pst)
{assert(pst);pst->arr = NULL;pst->top = pst->capacity = 0;
}//入栈
void stack_push(stack* pst, st_data_type x)
{assert(pst);if (pst->capacity == pst->top){int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;st_data_type* tmp = (st_data_type*)realloc(pst->arr, newcapacity * sizeof(st_data_type));if (tmp == NULL){perror("realloc fail!");return;}pst->arr = tmp;tmp = NULL;pst->capacity = newcapacity;}pst->arr[pst->top] = x;pst->top++;
}//出栈
void stack_pop(stack* pst)
{assert(pst);assert(pst->top > 0);pst->top--;
}//取出栈顶元素
st_data_type stack_top(stack* pst)
{assert(pst);assert(pst->top > 0);return pst->arr[pst->top-1];
}//销毁
void stack_destroy(stack* pst)
{assert(pst);free(pst->arr);pst->arr = NULL;pst->capacity = pst->top = 0;
}//判空
bool stack_empty(stack* pst)
{assert(pst);return pst->top == 0;
}//获取元素个数
int stack_size(stack* pst)
{assert(pst);return pst->top;
}typedef struct {stack st1;stack st2;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* pqu = (MyQueue*)malloc(sizeof(MyQueue));stack_init(&pqu->st1);stack_init(&pqu->st2);return pqu;
}void myQueuePush(MyQueue* obj, int x) {if (stack_empty(&obj->st1)){stack_push(&obj->st2, x);}else{stack_push(&obj->st1, x);}
}int myQueuePop(MyQueue* obj) {stack* empty = &obj->st1;stack* noempty = &obj->st2;if (stack_empty(&obj->st2)){empty = &obj->st2;noempty = &obj->st1;}while (stack_size(noempty) > 1){stack_push(empty, stack_top(noempty));stack_pop(noempty);}int top = stack_top(noempty);stack_pop(noempty);while (!stack_empty(empty)){stack_push(noempty, stack_top(empty));stack_pop(empty);}return top;
}int myQueuePeek(MyQueue* obj) {stack* empty = &obj->st1;stack* noempty = &obj->st2;if (stack_empty(noempty)){empty = &obj->st2;noempty = &obj->st1;}while (!stack_empty(noempty)){stack_push(empty, stack_top(noempty));stack_pop(noempty);}int top = stack_top(empty);while (!stack_empty(empty)){stack_push(noempty, stack_top(empty));stack_pop(empty);}return top;
}bool myQueueEmpty(MyQueue* obj) {return stack_empty(&obj->st1) && stack_empty(&obj->st2);
}void myQueueFree(MyQueue* obj) {stack_destroy(&obj->st1);stack_destroy(&obj->st2);free(obj);
}

方法二:正是因为栈后进先出的特点,我们可以不用将导过来的数据再导回去,一个栈专门用来入数据,另一个栈专门用来出数据。 很显然这种方法更为简单。

typedef int st_data_type;typedef struct stack
{st_data_type* arr;int top;int capacity;
}stack;
void stack_init(stack* pst)
{assert(pst);pst->arr = NULL;pst->top = pst->capacity = 0;
}//入栈
void stack_push(stack* pst, st_data_type x)
{assert(pst);if (pst->capacity == pst->top){int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;st_data_type* tmp = (st_data_type*)realloc(pst->arr, newcapacity * sizeof(st_data_type));if (tmp == NULL){perror("realloc fail!");return;}pst->arr = tmp;tmp = NULL;pst->capacity = newcapacity;}pst->arr[pst->top] = x;pst->top++;
}//出栈
void stack_pop(stack* pst)
{assert(pst);assert(pst->top > 0);pst->top--;
}//取出栈顶元素
st_data_type stack_top(stack* pst)
{assert(pst);assert(pst->top > 0);return pst->arr[pst->top-1];
}//销毁
void stack_destroy(stack* pst)
{assert(pst);free(pst->arr);pst->arr = NULL;pst->capacity = pst->top = 0;
}//判空
bool stack_empty(stack* pst)
{assert(pst);return pst->top == 0;
}//获取元素个数
int stack_size(stack* pst)
{assert(pst);return pst->top;
}typedef struct {stack pushst;stack popst;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* pst = (MyQueue*)malloc(sizeof(MyQueue));stack_init(&pst->pushst);stack_init(&pst->popst);return pst;
}void myQueuePush(MyQueue* obj, int x) {stack_push(&obj->pushst, x);
}int myQueuePop(MyQueue* obj) {if (stack_empty(&obj->popst)){while (!stack_empty(&obj->pushst)){stack_push(&obj->popst, stack_top(&obj->pushst));stack_pop(&obj->pushst);}}int top = stack_top(&obj->popst);stack_pop(&obj->popst);return top;
}int myQueuePeek(MyQueue* obj) {if (stack_empty(&obj->popst)){while (!stack_empty(&obj->pushst)){stack_push(&obj->popst, stack_top(&obj->pushst));stack_pop(&obj->pushst);}}return stack_top(&obj->popst);
}bool myQueueEmpty(MyQueue* obj) {return stack_empty(&obj->pushst) && stack_empty(&obj->popst);
}void myQueueFree(MyQueue* obj) {stack_init(&obj->pushst);stack_init(&obj->popst);free(obj);
}

💥9、设计循环队列

  • Leetcode——设计循环队列

在这里插入图片描述

这里我们用数组来实现循环队列会相对简单一些,让head指向第一个位置,让tail指向最后一个元素的下一个位置。假设队列长度为K,我们开K + 1个空间,多开一个空间是为了方便区分队列为空和队列为满。也可以在结构体中多加一个变量用来计数。因为如果不多开一个空间,队列为空时是head == tail,队列为满时也是head == tail,无法区分。多开一个空间后,队列为满就是head == (tail + 1) % (k + 1).
tail越界时我们对其模K+1,让tail指向下标为0的位置。
在这里插入图片描述

typedef struct {int* a;int head;int tail;int k;
} MyCircularQueue;bool myCircularQueueIsEmpty(MyCircularQueue* obj) {return obj->head == obj->tail;
}bool myCircularQueueIsFull(MyCircularQueue* obj) {return obj->head == (obj->tail + 1) % (obj->k + 1);
}MyCircularQueue* myCircularQueueCreate(int k) {MyCircularQueue* pq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));pq->a = (int*)malloc(sizeof(int)*(k + 1));pq->head = pq->tail = 0;pq->k = k;return pq;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {if (myCircularQueueIsFull(obj)){return false;}obj->a[obj->tail++] = value;obj->tail %= obj->k + 1;return true;
}bool myCircularQueueDeQueue(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj)){return false;}obj->head++;obj->head %= obj->k + 1;return true;
}int myCircularQueueFront(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj)){return -1;}return obj->a[obj->head];
}int myCircularQueueRear(MyCircularQueue* obj) {if (myCircularQueueIsEmpty(obj)){return -1;}return obj->a[(obj->tail - 1 + obj->k + 1) % (obj->k + 1)];
}void myCircularQueueFree(MyCircularQueue* obj) {free(obj->a);free(obj);
}

这篇关于【C++】OJ习题 篇2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124404

相关文章

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C