我不知道怎样的结局,才能配得上这一路的颠沛流离.
趁着你的能力还能撑得起你的野心,那就只顾着风雨兼程!
#include <stdio.h>
#include <stdlib.h>
#pragma once
1.括号匹配问题
解题思路
- 遍历字符串, 遇到一个字符:
- a.如果该括号是左括号("([{"), 入栈;
- b.如果该括号不是左括号, 检查该括号是否匹配与栈顶括号;
- 确认栈中是否有元素,如果有,用当前括号与栈顶的括号进行比较, //匹配则继续,否则返回flase.
bool isValid(char* s){
if (NULL == s){
return true;
}
Stack st;
StackInit(&s);
int len = strlen(s);
for (int i = 0; i < len; ++i){
//左括号入栈
if ('(' == s[i] || '[' == s[i] || '{' == s[i]){
StackPush(&st, s[i]);
}
else{
//s[i]为右括号
if (StackEmpty(&st)){
return false;
}
char ch = StackTop(&st);
//检测左右括号是否匹配
if ('(' == ch && ')' == s[i] || '[' == ch && ']' == s[i]
|| '{' == ch && '}' == s[i]){
StackPop(&st);
}
else{
return false;
}
}
}
if (!StackEmpty(&st)){
return false;
}
return true;
}
2.用队列实现栈
typedef struct {
Queue _q1;
Queue _q2;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {
MyStack* pms = (MyStack*)malloc(sizeof(MyStack));
if (NULL==pms){
assert(0);
return NULL;
}
QueueInit(&pms->_q1);
QueueInit(&pms->_q2);
return pms;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
assert(obj);
if (QueueEmpty(&obj->_q1)){
QueuePush(&obj->_q2,x);
}
else{
QueuePush(&obj->_q1,x);
}
}
/** Removes the element on top of the stack and returns that element.*/
int myStackPop(MyStack* obj) {
assert(obj);
if (!QueueEmpty(&obj->_q1)){
//将q1中的前n-1个元素移动到q2中
int size = QueueSize(&obj->_q1);
while (size > 1){
QueuePush(&obj->_q2,QueueFront(&obj->_q1));
QueuePop(&obj->_q1);
size--;
}
int ret = QueueBack(&obj->_q1);
QueuePop(&obj->_q1);
return ret;
}
else{
//将q2中的前n-1个元素移动到q1中
int size = QueueSize(&obj->_q2);
while (size > 1){
QueuePush(&obj->_q1, QueueFront(&obj->_q2));
QueuePop(&obj->_q2);
size--;
}
int ret = QueueBack(&obj->_q2);
QueuePop(&obj->_q2);
return ret;
}
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
if (QueueEmpty(&obj->_q1)){
return QueueBack(&obj->_q2);
}
else{
return QueueBack(&obj->_q1);
}
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->_q1) && QueueEmpty(&obj->_q2);
}
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->_q1);
QueueDestroy(&obj->_q2);
free(obj);
}
3.用栈实现队列
解题思路
- (1)入队列:1, 2, 3, 4(直接将数据放到s1中)
- (2)出队列(检测s2中是否有数据) 如果有,则直接出 如果为空,则将s1中的数据搬移到s2中
- (3)获取队头元素 (检测s2中是否有数据) 如果有:则直接返回如果没有:将s1中的数据搬移到s2中
- (4)检测队列是否为空:s1&&s2
typedef struct {
Stack _s1; //模拟入队列操作
Stack _s2; //模拟出队列操作
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
MyQueue* pmq = (MyQueue*)malloc(sizeof(MyQueue));
if (NULL == pmq){
assert(0);
return NULL;
}
StackInit(&pmq->_s1);
StackInit(&pmq->_s2);
return pmq;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
assert(obj);
StackPush(&obj->_s1, x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
assert(obj);
if (StackEmpty(&obj->_s2)){
//将s1中的元素搬移到s2中
while (!StackEmpty(&obj->_s1)){
StackPush(&obj->_s2, StackTop(&obj->_s2));
StackPop(&obj->_s1);
}
}
int ret = StackTop(&obj->_s2);
StackPop(&obj->_s2);
return ret;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
assert(obj);
if (StackEmpty(&obj->_s2)){
//将s1中的元素搬移到s2中
while (!StackEmpty(&obj->_s1)){
StackPush(&obj->_s2, StackTop(&obj->_s2));
StackPop(&obj->_s1);
}
}
return StackPop(&obj->_s2);
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->_s1) && StackEmpty(&obj->_s2);
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->_s1);
StackDestroy(&obj->_s2);
free(obj);
}
4.实现一个最小栈.
//解题思路:
(1)给入两个栈模拟实现, s1(保存数据), s2(保存最小值);
(2)入栈:
s1 : 每次都需要一个元素
s2 : data <= 栈中的最小值, s2入元素;
(3)出栈 :
s1与s2栈顶元素相等的情况下 :
s1与s2同时出栈; 否则是s1出栈;
typedef struct{
Stack ds; //存储数据
Stack ms; //存储最小值
}MinStack;
MinStack* minStackCreate(){
//MinStack ms; ms是函数中的一个临时变量,一旦函数运行结束,该变量就被销毁
MinStack* pms = (MinStack*)malloc(sizeof(MinStack));
if (NULL == pms){
assert(pms);
return NULL;
}
StackInit(&pms->ds);
StackInit(&pms->ms);
return pms;
}
void minStackPush(MinStack* obj, int x){
StackPush(&obj->ds, x);
if (StackEmpty(&obj->ms) || StackPop(&obj->ms)){
StackPush(&obj->ms, x);
}
}
void minStackPop(MinStack* obj){
if (StackTop(&obj->ms) == StackTop(&obj->ds)){
StackPop(&obj->ms);
}
StackPop(&obj->ds);
}
int minStackTop(MinStack* obj){
return StackTop(&obj->ds);
}
int minStackGetMin(MinStack* obj){
return StackTop(&obj->ms);
}
void minStackFree(MinStack* obj){
StackDestroy(&obj->ds);
StackDestroy(&obj->ms);
free(obj);
}
5.设计循环队列
typedef struct {
int* _array;
int _capacity;
int _size; //记录有效元素的个数
int _front;
int _back;
} MyCircularQueue;
/** Initialize your data structure here. Set the size of the queue to be k. */
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* q = (MyCircularQueue*)
malloc(sizeof(MyCircularQueue));
if (NULL == q){
assert(0);
retunr NULL;
}
q->_array = (int*)malloc(sizeof(int)*k);
if (NULL == q->_array){
assert(0);
return NULL;
}
q->_capacity = k;
q->_size = 0;
q->_front = 0;
q->_back = 0;
return q;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
assert(obj);
//检测队列是否满
if (obj->_size == obj->_capacity){
return false;
}
obj->_capacity[obj->_back++] = value;
obj->_size++;
if (obj->_back == obj->_capacity){
obj->_back = 0;
}
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
assert(obj);
//检测队列是或否为空
if (0 == obj->_size){
return false;
}
++obj->front;
if (obj->_front == obj->_capacity){
obj->_front = 0;
}
obj->_size--;
return true;
}
/** Get the front item from the queue. */
int myCircularQueueFront(MyCircularQueue* obj) {
if (0 == obj->_size){
return -1;
}
return obj->_array[obj->_front];
}
/** Get the last item from the queue. */
int myCircularQueueRear(MyCircularQueue* obj) {
if (o == obj->_size){
return -1;
}
if (0 == obj->_back){
return obj->_array[obj->_capacity - 1];
}
else{
return obj->_array[obj->_back - 1];
}
}
/** Checks whether the circular queue is empty or not. */
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return 0 == obj->_size;
}
/** Checks whether the circular queue is full or not. */
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return obj->_size == obj->_capacity;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->_array);
free(obj);
}
6.使用栈将递归转化为循环
void ReversePrint(Node* pHead){
if (NULL == pHead){
return;
}
Stack s;
StackInit(&s);
//遍历链表,将链表中的元素放到stack中
Node* pCur = pHead;
while (pCur){
StackPush(&s, pCur->data);
pCur->next;
}
//将栈中所有的元素打印
while (!StackEmpty(&s)){
printf("%d\n", StackTop(&s));
}
StackPop(&s);
StackDestroy(&s);
}