[LeetCode] Implement strStr()

Problem

Implement strStr().code

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.string

Note

有substring,爲什麼不用。io

Solution

public class Solution {
    public int strStr(String h, String n) {
        int len = n.length();
        for (int i = 0; i <= h.length()-len; i++) {
            if (h.substring(i, i+len).equals(n)) return i;
        }
        return -1;
    }
相關文章
相關標籤/搜索