Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc4ec5d

Browse files
Merge pull request doocs#226 from huaxu1024/master
Add Solution.java for 0557.Reverse Words in a String III
2 parents 3a791e1 + 1c2b4a0 commit bc4ec5d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Reverse Words in a String III
2+
3+
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.
4+
5+
## Example 1:
6+
```
7+
Input: "Let's take LeetCode contest"
8+
Output: "s'teL ekat edoCteeL tsetnoc"
9+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String reverseWords(String s) {
3+
String flag = " ";
4+
StringBuilder result = new StringBuilder();
5+
for (String temp : s.split(flag)) {
6+
for (int i = temp.length() - 1; i >= 0; i--) {
7+
result.append(temp.charAt(i));
8+
}
9+
result.append(flag);
10+
}
11+
return result.toString().substring(0, s.length());
12+
}
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /