LeetCode中Palindrome Number的C语言代码

由于要判断一个int型的整数是否是回文数,又不能增加额外的空间,所以用队列和用栈的做法不太可行,我的想法是把这个数倒过来,与输入进行比较,相同,则输入是回文数

代码如下

bool isPalindrome(int x) {
    if(x < 0)
        return false;
    long answer = 0;
    int temp = x;
    while(temp != 0){
        answer = answer*10 + temp%10;
        temp = temp/10;
    }
    if(answer < -2147483648 || answer > 2147483647)
        return false;
    
    return answer == x;
}

由于将一个整数颠倒过来,需要进行越界判断,因此answer用了long型变量,但accepted之后,发现这样的做法并不是最优算法,我也还没想到最优算法,如若有优化方法,会在写文章。

好的,以下是LeetCode 635题用C语言实现的代码,希望能够帮到您。 题目描述: 设计一个简易日志系统,支持以下操作: 1. 新增一条日志到日志系统。 2. 统计日志系统某个给定时间段内的日志数量。 3. 统计日志系统所有日志的数量。 实现: ```c typedef struct { char* id; int timestamp; } Log; typedef struct { Log* logs; int size; int capacity; } LogSystem; LogSystem* logSystemCreate() { LogSystem* obj = (LogSystem*)malloc(sizeof(LogSystem)); obj->logs = (Log*)malloc(sizeof(Log) * 1001); obj->size = 0; obj->capacity = 1001; return obj; } void logSystemAdd(LogSystem* obj, int id, char* timestamp) { obj->logs[obj->size].id = (char*)malloc(sizeof(char) * 15); sprintf(obj->logs[obj->size].id, "%d", id); char year[5], month[3], day[3], hour[3], minute[3], second[3]; strncpy(year, timestamp, 4); year[4] = '\0'; strncpy(month, timestamp + 5, 2); month[2] = '\0'; strncpy(day, timestamp + 8, 2); day[2] = '\0'; strncpy(hour, timestamp + 11, 2); hour[2] = '\0'; strncpy(minute, timestamp + 14, 2); minute[2] = '\0'; strncpy(second, timestamp + 17, 2); second[2] = '\0'; obj->logs[obj->size].timestamp = atoi(year) * 100000000 + atoi(month) * 1000000 + atoi(day) * 10000 + atoi(hour) * 100 + atoi(minute); obj->size++; } int* logSystemRetrieve(LogSystem* obj, char* s, char* e, char* gra, int* returnSize) { int start, end, len; int* res = (int*)malloc(sizeof(int) * obj->size); *returnSize = 0; if (strcmp(gra, "Year") == 0) { start = atoi(strncpy(s, s, 4)) * 1000000; end = atoi(strncpy(e, e, 4)) * 1000000; len = 4; } else if (strcmp(gra, "Month") == 0) { start = atoi(strncpy(s, s, 7)) * 10000; end = atoi(strncpy(e, e, 7)) * 10000; len = 7; } else if (strcmp(gra, "Day") == 0) { start = atoi(strncpy(s, s, 10)) * 100; end = atoi(strncpy(e, e, 10)) * 100; len = 10; } else if (strcmp(gra, "Hour") == 0) { start = atoi(strncpy(s, s, 13)); end = atoi(strncpy(e, e, 13)); len = 13; } else if (strcmp(gra, "Minute") == 0) { start = atoi(strncpy(s, s, 16)); end = atoi(strncpy(e, e, 16)); len = 16; } else { start = atoi(strncpy(s, s, 19)); end = atoi(strncpy(e, e, 19)); len = 19; } for (int i = 0; i < obj->size; i++) { int time = obj->logs[i].timestamp; if (time >= start && time < end) { res[(*returnSize)++] = atoi(obj->logs[i].id); } } int* result = (int*)malloc(sizeof(int) * (*returnSize)); for (int i = 0; i < *returnSize; i++) { result[i] = res[i]; } free(res); return result; } void logSystemFree(LogSystem* obj) { for (int i = 0; i < obj->size; i++) { free(obj->logs[i].id); } free(obj->logs); free(obj); } ``` 这段代码实现了一个简单的日志系统,包含了新增日志、统计日志数量等操作。在这个实现,我们使用了结构体Log和LogSystem来表示日志和日志系统。其,Log包含了日志的id和时间戳,LogSystem包含了日志数组、数组大小和数组容量。 在新增日志的操作,我们将日志的id和时间戳存储到日志对象,同时将日志对象存储到日志数组。 在统计日志数量的操作,我们首先根据粒度参数gra将起始时间s和结束时间e转换成整数形式,然后遍历日志数组,统计符合要求的日志数量。 最后,在释放日志系统对象的操作,我们需要将每个日志对象的id字符串释放掉,并释放日志数组和日志系统对象本身所占用的内存。 希望这段代码能够帮到您,如果您有任何问题或疑问,请随时向我提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值