现在的位置: 首页 > 综合 > 正文

Reverse Words in a String

2018年04月29日 ⁄ 综合 ⁄ 共 510字 ⁄ 字号 评论关闭

快特么气成傻逼了才AC

#include <iostream>
#include <stack>
using namespace std;

stack<string> st;

class Solution {
public:
    void reverseWords(string &s) {
		int i = s.find_first_not_of(" ");
		if(i > s.size())
		{
			s = "";
			return;
		}
		string t;
		while(i < s.size())
		{
			if(s[i] != ' ')
			{
				t += s[i];	
			}
			else
			{
				if(!t.empty())
				{
					st.push(t);
					t = "";
				}
			}
			i++;
		}
		if(!t.empty())
		{
			st.push(t);
			t = "";
		}
		string re;
		while(!st.empty())
		{
			re += st.top();
			st.pop();
			re += " ";	
		}
		re.erase(re.find_last_not_of(" ")+1);
		s = re;
	}
};

int main(void)
{
	//string a = "the sky is blue";
	string a = "a";
	Solution s;
	s.reverseWords(a);

	cout << a << endl;
	
	return 0;
}

抱歉!评论已关闭.