File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff 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++){
Original file line number Diff line number Diff line change 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+
75103main ()
76104{
77105 string s;
You can’t perform that action at this time.
0 commit comments