首页 > 其他 > 详细

Implement strStr()

时间:2015-05-06 01:22:32      阅读:204      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/implement-strstr/

Implement strStr().

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

 1 public class Solution {
 2     public static int strStr(String haystack, String needle) {
 3         if(needle.equals("")){return 0;}
 4         int len1=haystack.length();int len2=needle.length();
 5         for(int i=0;i+len2<=len1;i++){
 6             if(haystack.charAt(i)!=needle.charAt(0)){continue;}
 7             boolean flag=true;
 8             for(int j=1;j<len2;j++){
 9             if(haystack.charAt(i+j)!=needle.charAt(j)){flag=false;break;}
10             }
11             if(flag==true){return i;}
12         }
13         return -1;
14     }
15     public static void main(String[]args){
16     System.out.println(strStr("dddabcfsdf","abc"));
17     }
18 }

 

Implement strStr()

原文:http://www.cnblogs.com/qq1029579233/p/4480678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!