Implement strStr()

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.


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


你可能感兴趣的:(java,LeetCode)