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

Implement strStr()

时间:2015-05-06 01:22:53      阅读:167      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!