【每日刷题】Day33

【每日刷题】Day33

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

[1. 20. 有效的括号 - 力扣(LeetCode)](#1. 20. 有效的括号 - 力扣(LeetCode))

[2. 445. 两数相加 II - 力扣(LeetCode)](#2. 445. 两数相加 II - 力扣(LeetCode))

[3. 1021. 删除最外层的括号 - 力扣(LeetCode)](#3. 1021. 删除最外层的括号 - 力扣(LeetCode))

1. 20. 有效的括号 - 力扣(LeetCode)

//思路:栈的基本使用。

typedef char STDataType;

typedef struct Stack

{

STDataType* arr;

int top;

int capacity;

}ST;

//初始化栈

void StackInit(ST* st)

{

st->arr = NULL;

st->top = 0;

st->capacity = 0;

}

//入栈

void StackPush(ST* st,STDataType s)

{

assert(st);

if(st->top==st->capacity)

{

int newcapacity = st->capacity == 0?4:2*st->capacity;

STDataType* tmp = (STDataType*)realloc(st->arr,newcapacity*sizeof(STDataType));

if(tmp==NULL)

{

perror("malloc:");

exit(-1);

}

st->capacity = newcapacity;

st->arr = tmp;

}

st->arrst-\>top = s;

st->top++;

}

//获取栈顶元素

STDataType StackTop(ST* st)

{

assert(st);

assert(st->top>0);

return st->arrst-\>top-1;

}

//出栈

void StackPop(ST* st)

{

assert(st);

assert(st->top>0);

st->top--;

}

//判断栈是否为空

bool StackEmpty(ST* st)

{

assert(st);

return st->top == 0;

}

bool isValid(char* s)

{

ST st;

StackInit(&st);//初始化栈

while(*s)

{

if(*s=='{'||*s=='['||*s=='(')//如果遇到左括号,则入栈

{

StackPush(&st,*s);

}

else//如果遇到右括号,将栈中元素出栈

{

if(StackEmpty(&st))//如果前面没有入过栈,则直接返回

{

return false;

}

STDataType tmp = StackTop(&st);//先取得栈顶元素,判断是否与右括号匹配

StackPop(&st);//出栈

if((tmp=='('&&*s!=')')//如果不匹配,直接返回

||(tmp=='{'&&*s!='}')

||(tmp==''\&\&\*s!=''))

{

return false;

}

}

s++;

}

bool ret = StackEmpty(&st);//判断栈是否为空,为空说明所有左括号均与右括号匹配,为true,否则为false

return ret;

}

2. 445. 两数相加 II - 力扣(LeetCode)

//思路:栈和链表的基本使用。

typedef struct ListNode LN;

int max(int x,int y)

{

return x>y?x:y;

}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)

{

LN* newhead = NULL;

int i = 0;

int j = 0;

int arr1100 = {0};

int arr2100 = {0};

LN* pmove1 = l1;

LN* pmove2 = l2;

int flag = 0;

while(pmove1)//链表1入栈

{

arr1i++ = pmove1->val;

pmove1 = pmove1->next;

}

while(pmove2)//链表2入栈

{

arr2j++ = pmove2->val;

pmove2 = pmove2->next;

}

i--;//定位栈顶元素

j--;

while(i>=0||j>=0)//两个栈同时出栈,元素相加,判断是否需要进位

{

LN* newnode = (LN*)malloc(sizeof(LN));//新节点,以头插的方式插入新链表

newnode->next = newhead;

newhead = newnode;

int x = i>=0?arr1i:0;//栈1元素

int y = j>=0?arr2j:0;//栈2元素

int tmp = (x+y+flag)%10;

newnode->val = tmp;

if(x+y+flag>=10)//判断是否进位

{

flag = 1;

}

else

{

flag = 0;

}

if(i>=0)//防止栈越界访问

{

i--;

}

if(j>=0)

{

j--;

}

}

if(flag)//如果出了循环,flag还为1,则说明最高位进了1,创建新节点,节点val为1,头插进链表中

{

LN* newnode = (LN*)malloc(sizeof(LN));

newnode->next = newhead;

newnode->val = 1;

newhead = newnode;

}

return newhead;

}

3. 1021. 删除最外层的括号 - 力扣(LeetCode)

//0ms 100%思路:与第一题类似,栈的基础使用。

typedef int STDataType;

//栈结构体

typedef struct Stack

{

STDataType* arr;

int top;

int capacity;

}ST;

//初始化栈

void StackInit(ST* st)

{

assert(st);

st->arr = NULL;

st->capacity = st->top = 0;

}

//释放栈

void StackRelease(ST* st)

{

assert(st);

free(st->arr);

st->arr = NULL;

st->capacity = st->top = 0;

}

//判空

bool StackEmpty(ST* st)

{

assert(st);

return st->top == 0;

}

//入栈

void StackPush(ST* st, STDataType x)

{

assert(st);

if (st->top == st->capacity)

{

int newcapacity = st->capacity == 0 ? 4 : 2 * st->capacity;

STDataType* tmp = (STDataType*)realloc(st->arr,sizeof(STDataType) * newcapacity);

if (tmp == NULL)

{

perror("malloc:");

exit(-1);

}

st->arr = tmp;

st->capacity = newcapacity;

}

st->arrst-\>top = x;

st->top++;

}

//出栈

void StackPop(ST* st)

{

assert(st);

assert(st->top > 0);

st->top--;

}

//获取栈顶元素

STDataType StackTop(ST* st)

{

assert(st);

assert(st->top > 0);

return st->arrst-\>top - 1;

}

char* removeOuterParentheses(char* s)

{

ST st;

StackInit(&st);//初始化栈

char* arr = (char*)malloc(sizeof(char)*100000);//新空间存储删除后的字符串

int count = 0;

while(*s)

{

if(*s=='(')//如果为左括号入栈

{

StackPush(&st,*s);

}

if(st.top>1)//如果栈中元素大于1,说明当前*s不是要删除的括号,直接放入创建好的空间中

{

arrcount++ = *s;

}

if(*s==')')//如果是右括号,与栈中左括号匹配,出栈

{

StackPop(&st);

}

s++;

}

arrcount = '\0';

return arr;

}

相关推荐
QiLinkOS9 小时前
第三视觉理解徐玉生与他的商业活动(30)
大数据·c++·人工智能·算法·开源协议
疯狂打码的少年9 小时前
【操作系统】页面置换算法(OPT/FIFO/LRU)
算法
小O的算法实验室9 小时前
2026年CIE,优化客货协同运输:综合地铁系统的列车容量动态分配
算法
Coder_Shenshen10 小时前
西门子S7CommPlus协议鉴权算法原理与流程详解
网络·后端·算法
硕风和炜11 小时前
【LeetCode: 2492. 两个城市间路径的最小分数 + DFS】
java·算法·leetcode·深度优先·dfs·bfs·并查集
我是一颗柠檬12 小时前
【Java项目技术亮点】加权轮询负载均衡算法
java·算法·负载均衡
灯厂码农12 小时前
C语言动态内存分配完全指南(malloc、calloc、realloc、free)
java·c语言·算法
wuyk55512 小时前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机
qq_2415856113 小时前
可用在中断中浮点数打印类似printf
c语言
凯瑟琳.奥古斯特13 小时前
K次取反最大化数组和解法(力扣1005)
开发语言·c++·算法·leetcode·职场和发展