0
点赞
收藏
分享

微信扫一扫

NC181 单词拆分(一)

NC181 单词拆分(一)_方法名
NC181 单词拆分(一)_c++_02
示例1

输入:
"nowcoder",["now","coder"]
返回值:
true

示例2

输入:
"nowcoder",["no","wcod","der"]
返回值:
false

示例3

输入:
"nowcodernow",["now","coder"]
返回值:
true

Code:
常规操作,不断删除容器中的元素即可

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param dic string字符串vector
* @return bool布尔型
*/
bool wordDiv(string s, vector<string>& dic) {
// write code here
string res="";
for(int i=0;i<dic.size();i++)
{
int pos=s.find(dic[i]);
if(pos!=string::npos)
{
s=s.erase(pos,dic[i].length());
}
else
return false;
}
return true;

}
};


举报

相关推荐

0 条评论