码迷,mamicode.com
首页 > 其他好文 > 详细

Implement strStr

时间:2014-11-13 18:21:30      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   sp   for   div   on   

Implement strStr()

 

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

这里用的BF算法实现的,KMP待写...

 1 public class Solution {
 2     public int strStr(String haystack, String needle) {
 3         boolean found = true;
 4         int index = -1;
 5         if(0 == haystack.length() && 0 == needle.length())
 6             return 0;
 7         if(0 == haystack.length() && 0 == needle.length())
 8             return index;
 9         
10         for(int i = 0; i <= haystack.length() - needle.length(); i++){
11             int k = i;
12             found = true;
13             for(int j = 0; j < needle.length(); j++){
14                 if(haystack.charAt(k) == needle.charAt(j)){
15                     k++;
16                     continue;
17                 }
18                 else{
19                     j = 0;
20                     found = false;
21                     break;
22                 }
23             }//for
24             if(found){
25                 index = i;
26                 break;
27             }
28         }
29         
30         return index;
31     }
32 }

 

Implement strStr

标签:style   blog   io   color   ar   sp   for   div   on   

原文地址:http://www.cnblogs.com/luckygxf/p/4095167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!