java [28。实现strStr()] #Leetcode #substring()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [28。实现strStr()] #Leetcode #substring()相关的知识,希望对你有一定的参考价值。

/*
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
*/

class Solution {
    public int strStr(String haystack, String needle) {
        for( int i = 0; i <= haystack.length()-needle.length(); i++) {
            if(haystack.substring(i,i + needle.length()).equals(needle))
                return i;
        }
        return -1;
    }
}
// substring的用法+遍历

以上是关于java [28。实现strStr()] #Leetcode #substring()的主要内容,如果未能解决你的问题,请参考以下文章

java 28.实现strStr()。java

java 28.实现strStr()。java

java 28.实现strStr()。java

java 28.实现strStr()。java

java [28。实现strStr()] #Leetcode #substring()

LeetCode-28. 实现strStr()(java)