标签:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:对应检查char是否一样,不一样直接跳出检查下一组。用boolean来判断这组是不是符合,如果符合直接输出。
119/145
public class Solution { public int strStr(String haystack, String needle) { if(haystack==null&&needle==null||haystack.length()<needle.length()){ return -1; } for(int i=0;i<=haystack.length()-needle.length();i++){ boolean check=true; for(int j=0;j<needle.length();j++){ if(needle.charAt(j)!=haystack.charAt(i+j)){ check=false; break; } } if(check==true){ return i; } } return -1; } }
标签:
原文地址:http://www.cnblogs.com/Machelsky/p/5944465.html