LeetCode-28.Implement strStr()

来源:互联网 发布:网络诈骗防范常识电影 编辑:程序博客网 时间:2024/05/21 22:21

https://leetcode.com/problems/implement-strstr/

Implement strStr().

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

public int StrStr(string haystack, string needle)     {        int n1 = haystack.Length;        int n2 = needle.Length;        if (n2 == 0)            return 0;        for (int i = 0; i <= n1-n2; i++)        {            if (haystack[i] == needle[0])            {                int j = 0;                while (j<n2&&haystack[i + j] == needle[j])                    j++;                if (j == n2)                    return i;            }        }        return -1;    }


0 0
原创粉丝点击