-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151.cpp
More file actions
36 lines (34 loc) · 898 Bytes
/
151.cpp
File metadata and controls
36 lines (34 loc) · 898 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
//
// 151.cpp
// leetcode
//
// Created by R Z on 2018/7/19.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <string>
using namespace std;
class Solution {
public:
void reverseWords(string &s) {
reverse(s.begin(), s.end());
int storeIndex = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
if (storeIndex != 0) s[storeIndex++] = ' ';
int j = i;
while (j < s.size() && s[j] != ' ') { s[storeIndex++] = s[j++]; }
reverse(s.begin() + storeIndex - (j - i), s.begin() + storeIndex);
i = j;
}
}
s.erase(s.begin() + storeIndex, s.end());
}
void reverseString(string &s ,int l, int r){
while(l<r){
char c=s[l];
s[l++]=s[r];
s[r--]=c;
}
}
};