"["
false
"[]"
true
要求:空间复杂度
,时间复杂度
。
bool isValid(char* s) {
int top=0;
for(int i=0;s[i];i++){
char c=s[i];
if(c=='('){
s[top++]=')';
}else if(c=='['){
s[top++]=']';
}else if(c=='{'){
s[top++]='}';
}else if(top==0||s[--top]!=c){//否则就是右括号才成功匹配了
return false;
}
}
return top==0;//栈为空就返回true
} typedef struct stack {
char arr[10000];
int index;
} stack;
stack st;
void push(char ch) {
st.arr[++st.index] = ch;
}
char pop(void) {
if (st.index == -1)
return 0;
else
return st.arr[st.index--];
}
char check(void) {
if (st.index == -1)
return 0;
else
return st.arr[st.index];
}
bool isValid(char* s ) {
st.index = -1;
int i = -1;
while (i++, s[i]) {
switch (s[i]) {
case '(':
case '[':
case '{': push(s[i]); break;
case ')':
if (check() == '(') pop();
else return false;
break;
case ']':
if (check() == '[') pop();
else return false;
break;
case '}':
if (check() == '{') pop();
else return false;
break;
}
}
if (check()) return false;
else return true;
}
int stack[10000]={0}, pointStack=0;
void push(int node ) {
int i;
if(pointStack+1>=sizeof(stack))
return;
stack[pointStack++]=node;
}
int pop() {
int node=0;
if(pointStack<=0)
return 0;
node = stack[pointStack-1];
stack[pointStack-1]=0;
pointStack--;
return node;
}
int top() {
return stack[pointStack-1];
}
int len() {
return pointStack;
}
bool isValid(char* s ) {
int i;
for(i=0;i<strlen(s);i++) {
if(s[i]=='(' || s[i]=='{' || s[i]=='[')
push(s[i]);
else if(s[i]==')' || s[i]=='}' || s[i]==']') {
switch (s[i]) {
case ')': if(top()=='(')
pop();
else
return false;
break;
case '}': if(top()=='{')
pop();
else
return false;
break;
case ']': if(top()=='[')
pop();
else
return false;
break;
default: return false;
}
}
else
return false;
}
if(len()!=0)
return false;
return true;
} #include <stdio.h>
#include <malloc.h>
#include <string.h>
//stdlib.h
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
char trans(char char_r) {
if (char_r == ')')return '(';
if (char_r == ']')return '[';
if (char_r == '}')return '{';
return 0;
}
bool isValid(char* s ) {
// write code here
char* p;
int index_qian[10000] = {0};
int i = 0, j = 0;
p = (char*)malloc(sizeof(char) * strlen(s));
p = s;
// printf("s长度%d\r\n",strlen(s));
for (i = 0; i < strlen(s); i++) {
if (p[i] == '(' || p[i] == '{' || p[i] == '[') {
index_qian[j] = i;
j++;//printf("正括号%c\r\n",p[i]);
continue;
}
if (p[i] == ')' || p[i] == '}' || p[i] == ']')
if (trans(p[i]) == p[index_qian[(j - 1)]]) {
j--;//printf("反括号%c\r\n",p[i]);
} else { //free(p);
return 0;
}
}
//printf("j为%d\r\n",j);
//free(p);
if (j > 0) return 0;
return 1;
} 求助大佬为什么一用free()就卡住了
typedef struct lnode {
char c;
struct lnode* next;
} node, * stack;
void Push(stack* p, char a) {
node* cur = (node*)malloc(sizeof(node));
if (cur == NULL) return;
cur->c = a;
cur->next = *p;
*p = cur;
}
void Pop(stack* p) {
*p = (*p)->next;
}
bool isValid(char* s) {
// write code here
stack top = NULL;
for (int i = 0; s[i] != '\0'; i++) {//左括号时入栈
if ((s[i] == '(') || (s[i] == '[') ||(s[i] == '{'))
Push(&top, s[i]);
else {//右括号时,有匹配的左括号则出栈没有则返回false
if(top == NULL) return false;
else if ((top->c == '(' && s[i] == ')')
|| (top->c == '[' && s[i] == ']')
||(top->c == '{' && s[i] == '}'))
Pop(&top);
else return false;
}
}
if (top) return false;
return true;
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
#include <stdbool.h>
#include <stdio.h>
bool isValid(char* s ) {
int len=strlen(s);
char* stack=(char*)malloc(sizeof(char)*len);
int top=-1;
for(int i=0;i<len;i++)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
{
s[++top]=s[i];
}
else{
if(top==-1)return false;
if(s[i]==')'){
if(s[top]=='(')top--;
else return false;
}
else if(s[i]==']')
{
if(s[top]=='[')top--;
else return false;
}
else if(s[i]=='}'){
if(s[top]=='{')top--;
else return false;
}
}
}
if(top==-1)return true;
else return false;
}
#include <stdbool.h>
bool isValid(char* s ) {
// write code here
//如果字符串为空,返回false
if(!s)
{
return false;
}
//定义p指针指向字符串,求字符串长度len
char*p=s;
int len=0;
while(*(p++)!='\0')
{
len++;
}
//如果字符串len不为偶数,即不合法
if(len%2)
{
return false;
}
//重新定义p,指向后半段第一个字符
p=s+(len/2);
//定义数组,模拟栈
char stack[10000];
int count=0;
//字符串后半段按顺序入栈
while(*p!='\0')
{
stack[count++]=*p;
p++;
}
//重新定义p指向字符串开头
p=s;
//比较将字符串前半段(前->后)与栈(后->前)内容对比
for(int i=len/2-1;i>=0;i--)
{
if((i==0)&&\
((*p=='('&&stack[i]==')')||\
(*p=='['&&stack[i]==']')||\
(*p=='{'&&stack[i]=='}')))
{
//一一对应,为真
return true;
}
p++;
}
return false;
} #define SMALL 81 //()
#define MEDIUM 184 //[]
#define LARGE 248 //{}
#define MAX_LEN 5000
char stack[MAX_LEN];
int s_top = -1;
bool isValid(char* s ) {
// write code here
int len = strlen(s);
for(int i=0;i<len ;i++)
{
if(s_top <=-1 || s[i] == '(' || s[i] == '{'|| s[i] == '[')
stack[++s_top] = s[i];
else
{
int sum = s[i]+stack[s_top];
if(sum == SMALL ||
sum == MEDIUM ||
sum == LARGE)
s_top--;
else
return false;
};
}
return s_top==-1;
} /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MaxSize 5000
typedef struct Stack {
char* base;
char* top;
int StackSize;
}* stack;
int InitStack(stack s) {
s->base = (char*)malloc(sizeof(char) * MaxSize);
if (!s->base) {
exit(-2);
}
s->top = s->base;
s->StackSize = MaxSize;
return 1;
}
void PushStack(stack s, char c) {
if (s->top - s->base == MaxSize) {
exit(-2);
}
*(s->top) = c;
++(s->top);
}
char PopStack(stack s) {
if (s->top == s->base) {
return ' ';
}
char c = *(s->top - 1);
--(s->top);
return c;
}
bool isValid(char* s) {
if (strlen(s) <= 1) {
return false;
}
stack sta = (struct Stack*)malloc(sizeof(struct Stack));
InitStack(sta);
char c;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
PushStack(sta, s[i]);
} else {
if (sta->top - sta->base == 0) {
return false;
}
c = PopStack(sta);
if (c == '(') {
if (s[i] != ')') {
return false;
}
} else if (c == '[') {
if (s[i] != ']') {
return false;
}
} else if (c == '{') {
if (s[i] != '}') {
return false;
}
}
}
}
if (sta->top - sta->base == 0) {
return true;
} else {
return false;
}
}