《leetCode》:Implement strStr()

来源:互联网 发布:方圆设计软件下载 编辑:程序博客网 时间:2024/06/10 20:57

题目:

Implement strStr().

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

public class Solution {    int strStr(String haystack, String needle) {        if (needle.length()==0)return 0;        int m = haystack.length(), n = needle.length();        if (m < n) return -1;        for (int i = 0; i <= m - n; ++i) {            int j = 0;            for (j = 0; j < n; ++j) {                if (needle.charAt(j) != haystack.charAt(i + j)) break;            }            if (j == n) return i;        }        return -1;    }};


原创粉丝点击