strStr

来源:互联网 发布:网络爬虫能干什么 编辑:程序博客网 时间:2024/05/29 06:37

For a given source string and a target string, you should output the first index(from 0) of target string in source string.

If target does not exist in source, just return -1.

Have you met this question in a real interview? Yes
Example
If source = “source” and target = “target”, return -1.

If source = “abcdabcdefg” and target = “bcd”, return 1.

Challenge
O(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)

class Solution {    /**     * Returns a index to the first occurrence of target in source,     * or -1  if target is not part of source.     * @param source string to be scanned.     * @param target string containing the sequence of characters to match.     */    public int strStr(String source, String target) {        if (source == null || target == null) {            return -1;        }        for (int i = 0; i < source.length() - target.length() + 1; i++) {            int j = 0;            for (j = 0; j < target.length(); j++) {                if (source.charAt(i + j) != target.charAt(j)) {                    break;                }            }            // finished loop, target found            if (j == target.length()) {                return i;            }        }        return -1;    }}
0 0
原创粉丝点击