-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path151(翻转句子中的单词).cpp
134 lines (118 loc) · 2.99 KB
/
151(翻转句子中的单词).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
#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<unordered_set>
#include<string>
#include<unordered_set>
#include<unordered_map>
#include<stack>
#include<algorithm>
#include<stdarg.h>
#include<queue>
#include<map>
#include <stdint.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};
struct Point {
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
};
// 翻转字符串中的单词
// 先翻转整个句子,再翻转每个单词
// 注意:清除多余的空格
class Solution {
public:
void reverseWords(string &s) {
// 取消首尾多余的空格
while(s.length() > 0 && s[0] == ' ')
s = s.substr(1, s.length() - 1);
while(s.length() > 0 && s[s.length() - 1] == ' ')
s = s.substr(0, s.length() - 1);
// 去掉中间多余的空格
for(int i=1; i<s.length(); i++) {
if(s[i] == ' ' && s[i+1] == ' ') {
s.erase(s.begin() + i);
continue;
}
if(s[i] == '\t') {
s[i] = ' ';
i--;
continue;
}
}
if(s.length() == 0)
return;
reverse(s.begin(), s.end());
int start = 0;
bool flag = false;
for(int i=0; i<s.length(); i++) {
if(s[i] == ' ') {
flag = true;
reverse(s.begin() + start, s.begin() + i);
while(s[i] == ' ')
i++;
start = i;
}
}
reverse(s.begin() + start, s.end());
}
};
int main() {
Solution so;
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
//head->next->next = new ListNode(3);
//head->next->next->next = new ListNode(4);
//head->next->next->next->next = new ListNode(5);
//head->next->next->next->next->next = new ListNode(6);
TreeNode* head1 = new TreeNode(1);
head1->left = new TreeNode(2);
head1->right = new TreeNode(2);
//head1->left->left = new TreeNode(2);
TreeLinkNode* root = new TreeLinkNode(1);
root->left = new TreeLinkNode(2);
root->right = new TreeLinkNode(3);
string s = " a b ";
//int a[8] = {3, 7, 6, 4, 2, 8, 7, 3};
int a[6] = {-1, 2, 3, 5, 6, 7};
vector<int> v(a, a+6);
so.reverseWords(s);
string s1 = "catsanddog";
unordered_set<string> words;
words.insert("cat");
words.insert("cats");
words.insert("and");
words.insert("sand");
words.insert("dog");
//vector<string> ret = so.wordBreak(s1, words);
system("pause");
return 0;
}