Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
暴力算法的复杂度是 O(m*n),代码如下。更高效的的算法有KMP算法、Boyer-Mooer算法和Rabin-Karp算法。面试中暴力算法足够了,一定要写得没有BUG。
{% if book.cpp %} {% codesnippet "./code/strstr-1.cpp", language="cpp" %}{% endcodesnippet %} {% endif %}
{% if book.cpp %} {% codesnippet "./code/strstr-2.cpp", language="cpp" %}{% endcodesnippet %} {% endif %}