C语言 | Leetcode C语言题解之第557题反转字符串中的单词III

题目:

题解:

cpp 复制代码
char* reverseWords(char* s) {
    int length = strlen(s);
    char* ret = (char*)malloc(sizeof(char) * (length + 1));
    ret[length] = 0;
    int i = 0;
    while (i < length) {
        int start = i;
        while (i < length && s[i] != ' ') {
            i++;
        }
        for (int p = start; p < i; p++) {
            ret[p] = s[start + i - 1 - p];
        }
        while (i < length && s[i] == ' ') {
            ret[i] = ' ';
            i++;
        }
    }
    return ret;
}
相关推荐
longlongzihan9 小时前
LeetCode 5. 最长回文子串:从暴力匹配到中心扩展的思维演进
c++·算法·leetcode
苦瓜打怪兽9 小时前
leetcode 热题100(11. 盛最多水的容器)
python·算法·leetcode
Escalating_xu10 小时前
【C 语言】深入理解指针(4):回调函数、qsort、void * 与泛型排序的模拟实现
java·c语言·开发语言
吠品10 小时前
GitHub 周榜拆解:决策小模型扎堆上位
c语言·开发语言·算法
小师兄吃牛肉11 小时前
C语言篇:内联函数
c语言·开发语言
白山编程大哥11 小时前
C语言篇:语法进阶
c语言·数据结构·算法
Logic10112 小时前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
山下梅子酒22512 小时前
洛谷-入门-B2081
c语言
wuminyu13 小时前
Virtual Thread重投递至ForkJoinPool任务队列过程解析
java·linux·c语言·jvm·c++
All for pursuit.13 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode