forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path186 Reverse Words in a String II.js
More file actions
37 lines (30 loc) · 1004 Bytes
/
186 Reverse Words in a String II.js
File metadata and controls
37 lines (30 loc) · 1004 Bytes
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
// Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
// The input string does not contain leading or trailing spaces and the words are always separated by a single space.
// For example,
// Given s = "the sky is blue",
// return "blue is sky the".
// Could you do it in-place without allocating extra space?
/**
* @param {character[]} str
* @return {void} Do not return anything, modify the string in-place instead.
*/
var reverseWords = function(str) {
var arr = str;
reverse(arr, 0, arr.length - 1);
var last = 0;
for(var i = 0; i <= arr.length; i++) {
if(arr[i] === ' ' || i === arr.length) {
reverse(arr, last, i - 1);
last = i + 1;
}
}
function reverse(arr, beg, end) {
while(beg < end) {
var tmp = str[beg];
str[beg] = str[end];
str[end] = tmp;
beg++;
end--;
}
}
};