forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseWordsInAString.cpp
168 lines (139 loc) · 3.82 KB
/
reverseWordsInAString.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Source : https://oj.leetcode.com/problems/reverse-words-in-a-string/
// Author : Hao Chen, Siwei Xu
// Date : 2014-06-16
/**********************************************************************************
*
* Given an input string, reverse the string word by word.
*
* For example,
* Given s = "the sky is blue",
* return "blue is sky the".
*
*
* Clarification:
*
* What constitutes a word?
* A sequence of non-space characters constitutes a word.
* Could the input string contain leading or trailing spaces?
* Yes. However, your reversed string should not contain leading or trailing spaces.
* How about multiple spaces between two words?
* Reduce them to a single space in the reversed string.
*
*
**********************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for std::reverse
using namespace std;
void reverseWords(string &s) {
bool wordStart = false;
vector<string> v;
const char *pHead =s.c_str();
const char *pStr, *pBegin, *pEnd;
for (pStr=pHead; *pStr!='\0'; pStr++) {
if (!isspace(*pStr) && wordStart == false){
wordStart = true;
pBegin = pStr;
continue;
}
if(isspace(*pStr) && wordStart==true){
wordStart=false;
pEnd = pStr;
v.insert(v.begin(), s.substr(pBegin-pHead, pEnd-pBegin) );
}
}
if (wordStart==true){
pEnd = pStr;
v.insert(v.begin(), s.substr(pBegin-pHead, pEnd-pBegin) );
}
if (v.size()>0){
s.clear();
char space=' ';
vector<string>::iterator it;
for (it=v.begin(); it!=v.end(); ++it) {
s = s + *it;
s.push_back(space);
}
s.erase(s.end()-1);
}else{
s.clear();
}
cout << "[" << s << "]" <<endl;
}
// inspired from <Programming Pearls> -- Handwaving
void reverseWords2(string &s) {
if (s.length() == 0) return;
string result = "";
if (s[s.length()-1] == ' ') {
int last = s.find_last_not_of(' ') + 1;
s.erase(last, s.length() - last);
}
int first = s.find_first_not_of(' ', 0);
while (first != string::npos) {
int wend = s.find(' ', first); // word end
if (wend == string::npos) wend = s.length();
string word = s.substr(first, wend - first);
reverse(word.begin(), word.end());
result += word;
first = s.find_first_not_of(' ', wend); // next word
if (first == string::npos) break;
result += ' ';
}
reverse(result.begin(), result.end());
s.swap(result);
}
// C solution in O(1) space
void reverse(char *b, char *e) {
for (--e; e - b > 0; b++, e--) {
char t = *b;
*b = *e;
*e = t;
}
}
void reverseWords(char *s) {
char *p = s, *ws = NULL, *last = s;
while (*p && *p == ' ') p++; // skip leading space
ws = p;
for ( ; *p; p++) {
while (*p && *p != ' ') p++; // find word end
reverse(ws, p);
strncpy(last, ws, p-ws);
last += (p-ws);
while (*p && *p == ' ') p++; // for next word
ws = p;
if (*p == '\0') break;
*last++ = ' ';
}
reverse(s, last);
*last = '\0';
}
void test() {
#define TEST(str) do { \
char* s = strdup(str); \
printf("\"%s\" => ", s); \
reverseWords(s); \
printf("\"%s\"\n\n", s); \
free(s); \
} while (0)
TEST(" the blue sky is blue ");
TEST(" ");
}
main()
{
string s;
reverseWords(s);
s=" ";
reverseWords(s);
s="1 ";
reverseWords(s);
s="love";
reverseWords(s);
s="i love cpp";
reverseWords(s);
test();
}