当前位置: 首页> 财经> 产业 > C语言 | Leetcode C语言题解之第385题迷你语法分析器

C语言 | Leetcode C语言题解之第385题迷你语法分析器

时间:2025/8/27 8:30:08来源:https://blog.csdn.net/m0_59237910/article/details/141910172 浏览次数:0次

题目:

题解:

#define MAX_NEST_LEVEL 50001struct NestedInteger* deserialize(char * s){if (s[0] != '[') {struct NestedInteger * ni = NestedIntegerInit();NestedIntegerSetInteger(ni, atoi(s));return ni;}struct NestedInteger **stack = (struct NestedInteger **)malloc(sizeof(struct NestedInteger *) * MAX_NEST_LEVEL);int top = 0;int num = 0;bool negative = false;int n = strlen(s);for (int i = 0; i < n; i++) {char c = s[i];if (c == '-') {negative = true;} else if (isdigit(c)) {num = num * 10 + c - '0';} else if (c == '[') {struct NestedInteger * ni = NestedIntegerInit();stack[top++] = ni;} else if (c == ',' || c == ']') {if (isdigit(s[i - 1])) {if (negative) {num *= -1;}struct NestedInteger * ni = NestedIntegerInit();NestedIntegerSetInteger(ni, num);NestedIntegerAdd(stack[top - 1], ni);}num = 0;negative = false;if (c == ']' && top > 1) {struct NestedInteger *ni = stack[top - 1];top--;NestedIntegerAdd(stack[top - 1], ni);}}}struct NestedInteger * res = stack[top - 1];free(stack);return res;
}
关键字:C语言 | Leetcode C语言题解之第385题迷你语法分析器

版权声明:

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

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

责任编辑: