LeetCode – 557. Reverse Words in a String III

題目來源:LeetCode – 557. Reverse Words in a String III


題目:

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

 

粗略翻譯:

題目會予一組字串,程式目的是要將字串的每個單字的字元反轉

且維持字元的大小寫跟順序,空格保持不變

例如字串如果是(aBc DEF),則回傳(cBa FED)

解法:

 

利用迴圈找出空白字元的位置,之後將空白字元之前的字元順序全倒過來
接著再繼續尋找下一個空白字元

網站:http://wp.mlab.tw/
GitHub:https://github.com/yoll522/LeetCode
程式碼:https://github.com/yoll522/LeetCode/tree/master/557.%20Reverse%20Words%20in%20a%20String%20III

Leave a Reply

你的電子郵件位址並不會被公開。 必要欄位標記為 *