Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
// O(nm) runtime, O(1) space – Brute force:
//实现strstr(). 返回needle(关键字)在haystack(字符串)中第一次出现的位置,如果needle不在haystack中,则返回-1。
//注:strstr()是c++中的一个函数
public class Solution { public int strStr(String haystack, String needle) { for (int i = 0;; i++) { for (int j = 0;; j++) { if (j == needle.length()) return i; if (i + j == haystack.length()) return -1; if (needle.charAt(j) != haystack.charAt(i + j)) break; } } } }