Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
只需要遍历一遍即可。public String strStr(String haystack, String needle) { int lenH = haystack.length(); int lenN = needle.length(); if(lenH < lenN ){ return null; } for(int i = 0; i < lenH - lenN + 1; i ++){ int j = 0; while(j < lenN){ if(haystack.charAt(i + j) != needle.charAt(j)){ break; } j ++; } if(j == lenN){ return haystack.substring(i, lenH); } } return null; }
Implement strStr(),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u010378705/article/details/31351701