-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path557.java
More file actions
37 lines (36 loc) · 1.28 KB
/
557.java
File metadata and controls
37 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public String reverseWords(String s) {
char[] c = s.toCharArray();
int wordIndex = 0;
int spaceIndex = 0;
while ((spaceIndex = s.indexOf(" ", spaceIndex)) != -1) {
reverse(c, wordIndex, spaceIndex-1);
spaceIndex++;
wordIndex = spaceIndex;
}
reverse(c, wordIndex, c.length - 1);
return String.valueOf(c);
}
private static void reverse(char[] c, int i, int j) {
while (i < j) {
char temp = c[i];
c[i] = c[j];
c[j] = temp;
i++;j--;
}
}
}
__________________________________________________________________________________________________
sample 37328 kb submission
class Solution {
public String reverseWords(String s) {
return String.join(" ",
Arrays.asList(s.split(" "))
.stream()
.map(sec->new StringBuilder(sec).reverse().toString())
.collect(Collectors.toList()));
}
}
__________________________________________________________________________________________________