https://oj.leetcode.com/problems/implement-strstr/
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
/**
* Copyright: NineChapter
* - Algorithm Course, Mock Interview, Interview Questions
* - More details on: http://www.ninechapter.com/
*/
public class Solution {
public String strStr(String haystack, String needle) {
if(haystack == null || needle == null) {
return null;
}
int i, j;
for(i = 0; i < haystack.length() - needle.length() + 1; i++) {
for(j = 0; j < needle.length(); j++) {
if(haystack.charAt(i + j) != needle.charAt(j)) {
break;
}
}
if(j == needle.length()) {
return haystack.substring(i);
}
}
return null;
}
}
【注意1】别提KMP算法
【注意2】coding_style:
1:if后面一定要有{ };2.:双目运算符两边加空格;3:标点之后加空格;4:用换行区分不同逻辑块;
5:一定要有“异常检测”和“正确的边界条件”。
【注意3】面试时候先说思路,然后直接敲代码,别写注释。