2017年12月13日 星期三

[LeetCode] 557. Reverse Words in a String III

轉自LeetCode

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:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
<Solution>

想法如下
  • 利用空白來切割字串
  • 反轉切割出來的子字串,再串起來
code 如下

C++

Java

沒有留言:

張貼留言