Share my code using java(concise I think)


  • 0
    V

    as shown below

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

Log in to reply
 

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