You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LeetCode 38题Count and Say的C语言解法存疑,附我的AC代码

关于LeetCode第38题Count and Say的C语言解法困惑

最近在刷LeetCode第38题Count and Say的时候,我用C语言写了解法,虽然AC了,但还是对其中的一些细节感到困惑。下面是我的AC代码(我自己写了main函数测试,提交时只需要复制countAndSay函数就行):

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

char* countAndSay(int n) {
    if( n == 1 ) return "1";
    char *cur = (char *)malloc(2*sizeof(char));
    char *res;
    cur[0]='1';
    cur[1]='\0';
    int len, idx, j, count;
    for(int i = 2; i <= n; ++i) {
        len = strlen(cur);
        res = (char *)malloc(len * 2 + 1);
        memset(res, '\0', l...

内容的提问来源于stack exchange,提问作者52coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:24:04