-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_word_in_a_string.cpp
More file actions
56 lines (43 loc) · 1.47 KB
/
Copy pathreverse_word_in_a_string.cpp
File metadata and controls
56 lines (43 loc) · 1.47 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// reverse_word_in_a_string.cpp
// leet_code
//
// Created by Ruikun Hong on 7/29/15.
// Copyright (c) 2015 Ruikun Hong. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
class Solution {
public:
void reverseWords(string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(std::isspace))));
int start = 0;
bool is_space = false;
for (int i = 0; i < s.size(); ++i) {
if (!is_space && (s[i] == ' ' )) {
reverse(s.begin()+start, s.begin() + i);
start = i + 1;
is_space = true;
} else if (is_space && s[i] == ' ') {
continue;
} else if (is_space) {
int _size = s.size();
s.erase(s.begin() + start, s.begin() + i);
start = i + 1 - (_size - s.size() + 1);
i = i - (_size - s.size() + 1);
is_space = false;
}
}
// reverse the last piece
reverse(s.begin() + start, s.end());
// reverse the whole string to get the required order
reverse(s.begin(), s.end());
// remove the leading space of the string
s.erase(s.begin(), std::find_if(s.begin(), s.end(), not1(std::ptr_fun<int, int>(std::isspace))));
}
};