Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
在字符串haystack中查找字符型needle,并返回第一次出现的位置。
解答:
c++中string类的查找操作,对应find函数。
代码:
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle=="")
return 0;
string::size_type pos=haystack.find(needle);
return (int)pos;
}
};