a solution for Java


  • 0
    public class Solution {
        public int strStr(String haystack, String needle) {
            if(haystack == null || needle == null){
                return 0;
            }
            int nLength = needle.length();
            int hLength = haystack.length();
            
            for(int i = 0; i <= hLength - nLength; i++) {
                if(haystack.substring(i, i + nLength).equals(needle)) {
                    return i;
                }
            }
            return -1;
            
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.