标签:
1 class Solution { 2 public: 3 string ReverseSentence(string str) { 4 if(str.length() < 1) 5 return str; 6 bool sp = 1; 7 for (int i = 0 ;i < str.length() ; ++i) 8 { 9 if(str[i] != ‘ ‘) 10 { 11 sp = 0; 12 break; 13 } 14 } 15 if(sp) 16 { 17 return str; 18 } 19 stringstream ss; 20 ss << str; 21 string stem; 22 stack<string> st; 23 while(ss>>stem) 24 { 25 st.push(stem); 26 } 27 stem = ""; 28 bool fir = 1; 29 while(!st.empty()) 30 { 31 if (fir) 32 { 33 fir = 0; 34 stem += st.top(); 35 } 36 else 37 { 38 stem += " " + st.top(); 39 } 40 st.pop(); 41 } 42 return stem; 43 } 44 };
标签:
原文地址:http://www.cnblogs.com/xiaoyesoso/p/5161165.html