LeetCode——Implement strStr()

来源:互联网 发布:a3两厢好还是三厢 知乎 编辑:程序博客网 时间:2024/06/07 22:59

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

原题链接:https://oj.leetcode.com/problems/implement-strstr/

题目:实现strStr()函数。返回子串在母串中到开始出现的位置。

最直接的暴力查找。

public static String strStr(String haystack, String needle) {if (haystack == null || needle == null) {return null;}int i, j;for (i = 0; i < haystack.length() - needle.length() + 1; i++) {for (j = 0; j < needle.length(); j++) {if (haystack.charAt(i + j) != needle.charAt(j))break;}if (j == needle.length())return haystack.substring(i);}return null;}


0 0
原创粉丝点击