strStr

 1 class Solution {
 2     /**
 3      * Returns a index to the first occurrence of target in source,
 4      * or -1  if target is not part of source.
 5      * @param source string to be scanned.
 6      * @param target string containing the sequence of characters to match.
 7      */
 8     public int strStr(String source, String target) {
 9         // write your code here
10         if (source == null && target == null){
11             return -1;
12         }
13         if (source == null){
14             return -1;
15         }
16         if (target == null){
17             return -1;
18         }
19         for (int i = 0; i < source.length() - target.length() + 1; i++){
20             int j = 0;
21             for ( ; j < target.length(); j++){
22                 if (source.charAt(i + j) != target.charAt(j)){
23                     break;
24                 }
25             }
26             if (j == target.length()){
27                     return i;
28                 }
29         }
30         return -1;
31     }
32 }

 

posted @ 2017-06-23 15:23  大腮鱼  阅读(122)  评论(0)    收藏  举报