LeetCode(28)--Implement strStr()

来源:互联网 发布:mac 打字突然变成韩文 编辑:程序博客网 时间:2024/06/08 07:20

这道题题意是找到字符串中,子字符串出现的位置,若没有包含子字符串则返回1。可以直接调用string的函数find(a,b),其中a为子字符串,b为起始位置,若为查找到则返回无穷大。
代码实现如下:

class Solution {public:    int strStr(string haystack, string needle) {        int res= haystack.find(needle, 0);        if(res==INT_MAX) res=-1;        return res;    }};
原创粉丝点击