Skip to content

Commit 009085f

Browse files
committed
Merge pull request #97 from xusiwei/master
new solution, typo fix
2 parents 3786095 + 1a06e15 commit 009085f

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

algorithms/cpp/maximalRectangle/maximalRectangle.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int maximalRectangle(vector<vector<char> > &matrix) {
5656
if (matrix.size()<=0 || matrix[0].size()<=0) return 0;
5757
int row = matrix.size();
5858
int col = matrix[0].size();
59-
vector< vector<int> > heights(row, vector<int> col);
59+
vector< vector<int> > heights(row, vector<int>(col));
6060

6161
int maxArea = 0;
6262
for(int i=0; i<row; i++){

algorithms/cpp/reverseWordsInAString/reverseWordsInAString.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Source : https://oj.leetcode.com/problems/reverse-words-in-a-string/
2-
// Author : Hao Chen
2+
// Author : Hao Chen, Siwei Xu
33
// Date : 2014-06-16
44

55
/**********************************************************************************
@@ -71,7 +71,35 @@ void reverseWords(string &s) {
7171
}
7272
cout << "[" << s << "]" <<endl;
7373
}
74-
74+
75+
// inspired from <Programming Pearls> -- Handwaving
76+
void reverseWords2(string &s) {
77+
if (s.length() == 0) return;
78+
79+
string result = "";
80+
if (s[s.length()-1] == ' ') {
81+
int last = s.find_last_not_of(' ') + 1;
82+
s.erase(last, s.length() - last);
83+
}
84+
85+
int first = s.find_first_not_of(' ', 0);
86+
while (first != string::npos) {
87+
int wend = s.find(' ', first); // word end
88+
if (wend == string::npos) wend = s.length();
89+
90+
string word = s.substr(first, wend - first);
91+
reverse(word.begin(), word.end());
92+
result += word;
93+
94+
first = s.find_first_not_of(' ', wend); // next word
95+
if (first == string::npos) break;
96+
97+
result += ' ';
98+
}
99+
reverse(result.begin(), result.end());
100+
s.swap(result);
101+
}
102+
75103
main()
76104
{
77105
string s;

0 commit comments

Comments
 (0)