leetcode #151 in cpp
2016-06-29 10:28
344 查看
Solution:
1. reversed the whole string
2. reversed each word in the reversed string.
3. remove multiple space between words.
Code:
class Solution {
public:
void reverseWords(string &s) {
if(s.empty()) return;
int head = 0;
int end = s.length() -1;
char temp;
//reverse the string
while(head<end){
temp = s[head];
s[head]= s[end];
s[end] = temp;
head++;
end --;
}
head = 0;
int temp_end;
//reverse each word in the reversed string
while(true){
while(head < s.length() && s[head] == ' '){
head++;
}
if(head >= s.length()) break;
end = head;
while(end < s.length() && s[end] != ' '){
end++;
}
end --;
temp_end = end;
while(head<end){
temp = s[head];
s[head] = s[end];
s[end] = temp;
head++;
end--;
}
head = temp_end + 1;
}
int cur = 0;
head = 0;
//reduce multiple " " between word.
while(head<s.length()){
if(s[head] != ' '){//move word to s[cur]. This step ensures that there are no multiple //spaces between words.
s[cur++] = s[head++];
}
else{//if we meet a " " at s[head],first find the next word
while(head < s.length() && s[head] == ' '){
head++;
}
if(head >= s.length()) break;
if(cur > 0){//if cur > 0, first append a " " at s[cur]. then append the word
s[cur] = ' ';
cur++;
}
}
}
s = s.substr(0,cur);
}
};
1. reversed the whole string
2. reversed each word in the reversed string.
3. remove multiple space between words.
Code:
class Solution {
public:
void reverseWords(string &s) {
if(s.empty()) return;
int head = 0;
int end = s.length() -1;
char temp;
//reverse the string
while(head<end){
temp = s[head];
s[head]= s[end];
s[end] = temp;
head++;
end --;
}
head = 0;
int temp_end;
//reverse each word in the reversed string
while(true){
while(head < s.length() && s[head] == ' '){
head++;
}
if(head >= s.length()) break;
end = head;
while(end < s.length() && s[end] != ' '){
end++;
}
end --;
temp_end = end;
while(head<end){
temp = s[head];
s[head] = s[end];
s[end] = temp;
head++;
end--;
}
head = temp_end + 1;
}
int cur = 0;
head = 0;
//reduce multiple " " between word.
while(head<s.length()){
if(s[head] != ' '){//move word to s[cur]. This step ensures that there are no multiple //spaces between words.
s[cur++] = s[head++];
}
else{//if we meet a " " at s[head],first find the next word
while(head < s.length() && s[head] == ' '){
head++;
}
if(head >= s.length()) break;
if(cur > 0){//if cur > 0, first append a " " at s[cur]. then append the word
s[cur] = ' ';
cur++;
}
}
}
s = s.substr(0,cur);
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解