leetcode 648

class Solution {
public:
	string replaceWords(vector<string>& dict, string sentence) {  // 一直显示运行中,但是在ide里实验test case没问题
		// 分割字符串
		string ans = "";
		int start = 0;
		for (int i = 0; i < sentence.size(); i++) 
		{
			if (sentence[i] == ' ' ||  i == sentence.size() - 1) 
			{
				string tmp = sentence.substr(start, i - start);
				cout << tmp << endl;
				// 匹配字符串
				for (int j = 0; j < dict.size(); j++) 
				{
					// 如果匹配上了
					if (tmp.find(dict[j]) != string::npos) 
					{
						ans = ans + dict[j] + " ";
						break;
					}
					if (j == dict.size() -1)
					{
						ans = ans + tmp + " ";
						cout << " ans ------------ " << ans << endl;
					}
						
				}
				// 更新start
				start = i + 1;
			}			
		}

		return ans.substr(0, ans.size()-1);
	}
};

猜你喜欢

转载自blog.csdn.net/weixin_36149892/article/details/80273662
今日推荐