leetcode: Reverse Words in a String | Sigmainfy 烟客旅人
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
Clarification:
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
leetcode Reverse Words in a String Solution and Precautions:
My first initial solution would take O(N) time and O(N) space which is quite straightforward to simulate the process and find each separate word and put it into std::vector, we can get the final desired string by appending the word one by one from the tail of the word vector. (Note: we don't have to use std::reverse to get the desired result)
Read full article from leetcode: Reverse Words in a String | Sigmainfy 烟客旅人
No comments:
Post a Comment