Java 6ms Solution


  • 1
    X
    public int strStr(String haystack, String needle) {
        if(needle.isEmpty()) return 0;
        int res=-1;
        for(int i=0;i<haystack.length()-needle.length()+1;i++){
            for(int j=0, k=i;j<needle.length();j++,k++){
                if(needle.charAt(j)==haystack.charAt(k)) res=i;
                else{
                    res=-1;
                    break;
                }
            }
            if(res!=-1) return res;
        }
        return res;
    }

Log in to reply
 

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