问题:
难度:medium
说明:
给出一个字符串,去掉首位空格,然后把里面用空格隔开的每个单词位置都反转,然后翻转后的字符串单词之间,只隔着一个空格。
问题链接:https://leetcode.com/problems/reverse-words-in-a-string/
输入案例:
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
我的代码:
首先我注意到了,用 c 写算法的人需要用时间复杂度 O(1),这就意味着,用一个数组处理绝对可以(c语言的字符串就一数组)。然后就可以想象自己写了个 log(n) 的算法就可以了。
编译原理里面有介绍可以用:开始指针 + 向前指针