5 lines Java codes and beats 33%, easy understantd.


  • 0
    Y
    Using String's subString and equals methods, as you konw String is stored by array of character, and it's equals method is high-effiency.
    
    public int strStr(String haystack, String needle) {
    		if("".equals(needle))  return 0;
    		for(int i = 0; i < haystack.length(); i++)
    			if(haystack.length()-i >= needle.length() && haystack.substring(i, i+needle.length()).equals(needle))
                            return i;
    		return -1;
    }

  • 0
    F
    public int strStr(String haystack, String needle) {
    	return haystack.indexOf(needle);
    }

Log in to reply
 

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