反向话的顺序在一个串顺序

由网友(帅哥,请留步)分享简介:我有这样的字符串S1 =我的名字是某某,我想扭转的话的顺序,使 S1 =ZYX是我的名字。 I have this string s1 = "My name is X Y Z" and I want to reverse the order of the words so that s1 = "Z Y X is na...

我有这样的字符串S1 =我的名字是某某,我想扭转的话的顺序,使 S1 =ZYX是我的名字

I have this string s1 = "My name is X Y Z" and I want to reverse the order of the words so that s1 = "Z Y X is name My".

我可以使用一个额外的数组做到这一点。我觉得很难,但有可能做到这一点就地(不使用额外的数据结构),并与时间复杂度为O(N)?

I can do it using an additional array. I thought hard but is it possible to do it inplace (without using additional data structures) and with the time complexity being O(n)?

推荐答案

反转整个字符串,然后反转每个单词的字母。

Reverse the entire string, then reverse the letters of each individual word.

第一遍之后的字符串将是

After the first pass the string will be

s1 = "Z Y X si eman yM"

和第二遍之后将是

s1 = "Z Y X is name My"
阅读全文

相关推荐

最新文章