1ms java solution


  • 0
    Y
    public int strStr(String haystack, String needle) {
        if(haystack==null||needle==null||haystack.length()<needle.length()) return -1;
        if(needle.equals("")) return 0;
        int result = 0;
        for(int i=0,j=0;i<haystack.length()-needle.length()+1;i++){
            if(haystack.charAt(i)==needle.charAt(j))
                if(haystack.substring(i,needle.length()+i).equals(needle))
                    return i;
        }
        return -1;
    }

  • 0
    B

    7 ms .
    --- Oct/6/2016


Log in to reply
 

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