当前位置: 首页 > news >正文

有效的括号长按键入验证外星语词典字符的最短距离用栈实现队列

有效的括号

来源:杭哥

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

bool isValid(char * s)
{int sz=strlen(s);char stack[sz];int k=0;for (int i=0;i<sz;i++){if (s[i]=='(' || s[i]=='[' || s[i]=='{'){stack[k++]=s[i];}else{if (k==0){return false;}else if (s[i]=='}' && stack[k-1]!='{'){return false;}else if (s[i]==']' && stack[k-1]!='['){return false;}else if (s[i]==')' && stack[k-1]!='('){return false;}k--;}}if (k!=0){return false;}return true;
}
我想说:
  1. 这时候就要用栈这种数据结构了,非常方便。


长按键入

来源:自己LeetCode刷题

925. 长按键入 - 力扣(LeetCode)

bool isLongPressedName(char * name, char * typed)
{int sz1=strlen(name);int sz2=strlen(typed);int i=0;int j=0;if (name[i]==typed[j]){i++;j++;}else{return false;}while(i<=sz1-1 && j<=sz2-1){if (name[i]==typed[j]){i++;j++;}else{if (typed[j]==typed[j-1]){j++;}else{return false;}}}if (i==sz1){if (j==sz2){return true;}char ch=name[sz1-1];while(j<sz2){if (typed[j]!=ch){return false;}j++;}   }else{return false;}return true;
}
我想说:
  1. 这道题目的话可以用双指针来做,逻辑关系想想清楚,然后代码能力稍微有一点的话就可以做出来


验证外星语词典

来源:自己LeetCode刷题

953. 验证外星语词典 - 力扣(LeetCode)

bool isAlienSorted(char ** words, int wordsSize, char * order)
{int alpha[26]={0};for (int i=0;i<26;i++){alpha[order[i]-'a']=i;}for (int i=0;i<wordsSize-1;i++){char* s1=words[i];char* s2=words[i+1];while (*s1!='\0' && *s2!='\0' && alpha[*s1-'a']==alpha[*s2-'a']){s1++;s2++;}if ((*s1=='\0' && *s2!='\0') || (*s1=='\0' && *s2=='\0')){continue;}else if (*s1!='\0' && *s2=='\0'){return false;}if (alpha[*s1-'a']<alpha[*s2-'a']){continue;}else if (alpha[*s1-'a']>alpha[*s2-'a']){return false;}}return true;
}
我想说:
  1. 字符与整数之间可以灵活转化,因为字符其实本质上就是ACSII码,就是整型。


字符的最短距离

来源:自己LeetCode刷题

821. 字符的最短距离 - 力扣(LeetCode)

typedef struct point 
{int a;int b;
}point;
int* shortestToChar(char * s, char c, int* returnSize)
{int sz=strlen(s);int* ans = (int*)malloc(sizeof(int)*sz);*returnSize=sz;for (int i=0;i<sz;i++){ans[i]=-1;}point queue[10000]={0};int hh=0;int tt=-1;for (int i=0;i<sz;i++){if (s[i]==c){tt++;queue[tt].a=i;queue[tt].b=0;ans[i]=0;}}while(hh<=tt){int aa=queue[hh].a;int bb=queue[hh].b;hh++;if (aa-1>=0 && ans[aa-1]==-1){ans[aa-1]=bb+1;tt++;queue[tt].a=aa-1;queue[tt].b=bb+1;}if (aa+1<sz && ans[aa+1]==-1){ans[aa+1]=bb+1;tt++;queue[tt].a=aa+1;queue[tt].b=bb+1;}}return ans;
}
我想说:
  1. 首先得提一下语法问题,当用malloc在内存的堆区开辟一块连续空间的时候,其实我们都知道与数组没有什么区别,但是如果想把这块堆区空间的值,比如说全部初始化成-1,不能memset(arr , 0xff , sizeof(arr)),bty数组这样子干没有问题。这个与数组是有区别的,只能for循环一个一个去初始化。

  1. 这个方法用到了队列

#define MIN(a,b) ((a)<(b)?(a):(b))
int* shortestToChar(char * s, char c, int* returnSize)
{int sz=strlen(s);int* ans = (int*)malloc(sizeof(int)*sz);*returnSize=sz;int idx=(-1)*sz;for (int i=0;i<sz;i++){if (s[i]==c){idx=i;}ans[i]=i-idx;}idx=2*sz;for (int i=sz-1;i>=0;i--){if (s[i]==c){idx=i;}ans[i]=MIN(ans[i],idx-i);}return ans;
}
我想说:
  1. 这种方法的话就比较新奇,先从左往右遍历,这时候我统计的是左端距离字符c最近是多少(只关注左端);然后我从右往左遍历,这时候我统计的是右端距离字符c最近是多少(只关注右端),注意:还要与之前的数值取小。

  1. 然后在遍历的时候一开始都是不知道字符c在哪里的,这时候就假定idx为-n 或者 2n


用栈实现队列

232. 用栈实现队列 - 力扣(LeetCode)

typedef int STDataType;
typedef struct Stack
{int top;int capacity;STDataType* p;
}ST;void STInit(ST* pst)
{assert(pst);pst->p = (STDataType*)malloc(sizeof(STDataType)*100);if (pst->p==NULL){perror("STInit::Malloc");return;}pst->top=0;pst->capacity=100;
}void STDestroy(ST* pst)
{assert(pst);free(pst->p);pst->p=NULL;pst->top=0;pst->capacity=0;
}void STPush(ST* pst, STDataType x)
{assert(pst);if (pst->top==pst->capacity){STDataType* pp=(STDataType*)realloc(pst->p,sizeof(STDataType)*(pst->capacity)*2);if (pp==NULL){perror("STPush::Realloc");return;}pst->p=pp;pst->capacity*=2;}pst->p[pst->top]=x;pst->top++;
}void STPop(ST* pst)
{assert(pst);assert(pst->top>0);pst->top--;
}bool STEmpty(ST* pst)
{assert(pst);return pst->top==0;
}int STTop(ST* pst)
{assert(pst);assert(pst->top>0);return pst->p[pst->top-1];
} int STSize(ST* pst)
{assert(pst);return pst->top;
}///typedef struct 
{ST pushst;ST popst;
} MyQueue;MyQueue* myQueueCreate() 
{MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));if (obj==NULL){perror("malloc::fail");return NULL;}STInit(&obj->pushst);STInit(&obj->popst);return obj;
}void myQueuePush(MyQueue* obj, int x) 
{assert(obj);STPush(&obj->pushst,x);
}int myQueuePop(MyQueue* obj) 
{assert(obj);if (STEmpty(&obj->popst)){int num=STSize(&obj->pushst);for (int i=0;i<num;i++){STDataType tmp=STTop(&obj->pushst);STPush(&obj->popst,tmp);STPop(&obj->pushst);}}int ans=STTop(&obj->popst);STPop(&obj->popst);return ans;
}bool myQueueEmpty(MyQueue* obj) 
{assert(obj);return (&(obj->pushst))->top == 0 && (&(obj->popst))->top == 0;
}int myQueuePeek(MyQueue* obj) 
{assert(obj);if (STEmpty(&obj->popst)){int num=STSize(&obj->pushst);for (int i=0;i<num;i++){STDataType tmp=STTop(&obj->pushst);STPush(&obj->popst,tmp);STPop(&obj->pushst);}}int ans=STTop(&obj->popst);return ans;
}void myQueueFree(MyQueue* obj) 
{assert(obj);STDestroy(&obj->pushst);STDestroy(&obj->popst);
}
我想说:
  1. 这道题的话,它的方法非常的奇特,一个栈就是定向很明确的,专门用来放入数据,另外一个栈专门用来弹出数据,这个不像之前的用队列去模拟栈,哪个队列不为空,我就往哪个队列里面去插入数据。

  1. 然后这边的话出数据的栈它里面的数据都是从入数据的栈那边倒过来的,然后等到你这个栈出数据如果出空了,就再从另外一个栈那边把数据给倒过来。在这个问题当中,两个栈的功能是极其明确的,是固定的。


http://www.lryc.cn/news/41829.html

相关文章:

  • 《前端开发者的进阶之路》
  • 为什么说网络安全是风口行业?是IT行业最后的红利?
  • 使用shell 脚本,批量解压一批zip文件,解压后的文件放在以原zip文件名前10个字符的文件夹中的例子
  • 01 | Msyql系统架构
  • Linux命令---设备管理
  • 前端入门:HTML5+CSS3+JAAVASCRIPT
  • 【头歌实验】课外作业一:开通ECS及使用Linux命令
  • CMSIS-RTOS2 RTX5移植到GD32L233
  • [网络原理] 网络中的基本概念
  • BeanPostProcessor原理分析
  • 人工智能和网络安全,应该如何选择?
  • Flink预加载分区维表,实时更新配置信息
  • 大数据现在找工作难么
  • 【Linux】学会这些基本指令来上手Linux吧
  • 【沐风老师】3DMAX交通流插件TrafficFlow使用方法详解
  • c#实现视频的批量剪辑
  • 小白怎么系统的自学计算机科学和黑客技术?
  • scheduler 的使用实验对比和总结(PyTorch)
  • vue2 虚拟列表(优化版)
  • 从应用层到MCU,看Windows处理键盘输入 [1.在应用层调试Notepad.exe (按键消费者)]
  • 什么是大数据?大数据能做什么
  • Git 和 GitHub 超入门指南(四)
  • Java 响应式编程 Reactor 框架
  • Hazel引擎学习(十一)
  • 深度学习(22):如何判断训练过程中深度学习模型损失值不再下降
  • 一个比较全面的C#公共帮助类
  • 人脸识别经典网络-MTCNN(含Python源码实现)
  • OpenCV入门(十八)快速学会OpenCV 17 直线检测
  • nginx快速入门.跟学B站nginx一小时精讲课程笔记
  • 内存泄漏定位工具之 valgrind