leetcode 28 Implement Strstr()

两个for

外层for负责处理原字符串

内层for负责处理匹配字符串

 1 class Solution {
 2     public int strStr(String haystack, String needle) {
 3         for(int i=0;; i++){
 4             for(int j=0;; j++){
 5                 if(j == needle.length())
 6                     return i;
 7                 if(i + j == haystack.length())
 8                     return -1;
 9                 if(haystack.charAt(i+j) != needle.charAt(j))
10                     break;
11             }
12         }
13     }
14 }
原文地址:https://www.cnblogs.com/hwd9654/p/10930101.html