leetcode周赛中的学习
第一题
学习到了大神的for增强的用法
题目
给你一个字符串
s
和一个字符串数组words
,请你判断s
是否为words
的 前缀字符串 。字符串
s
要成为 words 的 前缀字符串 ,需要满足:s
可以由words
中的前k
(k
为 正数 )个字符串按顺序相连得到,且k
不超过words.length
。如果
s
是words
的 前缀字符串 ,返回true
;否则,返回false
。
我的代码
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
string temp=words.front(),temp1;
vector<string>::iterator it;
if(s==temp)
return true;
for(it=words.begin()+1;it<words.end();++it)
{
temp1=*it;
temp+=temp1;
if(s==temp)
return true;
}
return false;
}
};
大神代码
class Solution {
public:
bool isPrefixString(string s, vector<string>& words) {
string p;
for(string t : words) {
p += t;
if(p == s) return 1;
}
return 0;
}
};