Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解法1:
C++1
Java123456789101112131415161718192021public class Solution { public int strStr(String haystack, String needle) { if (needle == null || haystack == null) { return -1; } int n = haystack.length(), m = needle.length(); if (m > n) { return -1; } for (int i = 0; i <= n - m; i++) { if (haystack.substring(i, i + m).equals(needle)) { return i; } } return -1; }}