LeetCode (Implement strStr())

来源:互联网 发布:中国建筑业数据库 编辑:程序博客网 时间:2024/06/16 17:44

Problem:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Solution:

class Solution {public:    int strStr(string haystack, string needle) {        if (haystack.size() < needle.size()) return -1;        int j = -1;        for (int i = 0; i <= haystack.size() - needle.size(); i++){            string s = haystack.substr(i, needle.size());            if (s == needle){                j = i;                break;            }        }        return j;    }};


0 0
原创粉丝点击