当前位置: 首页> 财经> 股票 > C语言 | Leetcode C语言题解之第394题字符串解码

C语言 | Leetcode C语言题解之第394题字符串解码

时间:2025/8/23 13:49:00来源:https://blog.csdn.net/m0_59237910/article/details/142036488 浏览次数:0次

题目:

题解:


#define N 2000typedef struct {int data[30];;int top;
} Stack;void push(Stack *s, int e) { s->data[(s->top)++] = e; }int pop(Stack *s) { return s->data[--(s->top)]; }//多位数字串转换成int
int strToInt(char *s)
{char val[] = {'\0', '\0', '\0', '\0'};int result = 0;for(int i = 0; isdigit(s[i]); ++i)val[i] = s[i];for(int i = strlen(val) - 1, temp = 1; i >= 0; --i, temp *= 10)result += ((val[i] - '0') * temp);return result;
}char* decodeString(char *s)
{Stack magnification; magnification.top = 0;Stack position; position.top = 0;char *result = (char*)malloc(sizeof(char) * N);char *rear = result;for(int i = 0; s[i] != '\0'; ) {if(isdigit(s[i])) {push(&magnification, strToInt(&s[i]));while(isdigit(s[i]))++i;}else if(s[i] == '[') {push(&position, rear - result);++i;}else if(s[i] == ']') {char *p = result + pop(&position);int count = (rear - p) * (pop(&magnification) - 1);for(; count > 0; --count)*(rear++) = *(p++);++i;}else*(rear++) = s[i++];}*rear = '\0';return result;
}
关键字:C语言 | Leetcode C语言题解之第394题字符串解码

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: