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;
}
相关推荐
旖旎夜光31 分钟前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
文丑颜不良啊1 小时前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt1 小时前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表
文丑颜不良啊1 小时前
LeetCode1341~1360题目解析
java·leetcode
海绵宝宝转agent2 小时前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
Pniubi3 小时前
力扣543二叉树的直径(深度优先搜索法(递归))
算法·leetcode·职场和发展
听风说ml12 小时前
socket编程 【C语言】
c语言·udp·tcp·socket编程
学逆向的14 小时前
C 局部静态变量
c语言·开发语言·网络安全
j7~19 小时前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
Shaoshing21 小时前
LeetCode100 42.接雨水
java·算法·leetcode