Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254336
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-04-09 20:27:25

//Implement strStr() Total Accepted: 46286 Total Submissions: 209890 My Submissions Question Solution 
//Implement strStr().
//
//Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
public class ImplementstrStr {


public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.print(strStr("mississippi", "pi"));
}
public static int strStr(String haystack, String needle) {
// know length
int len=needle.length();
 if(haystack.length()  return -1;
 if(haystack.length()==len)
 return haystack.equals(needle)?0:-1;
     
     int i;
     //=
     for(i=0;i<=haystack.length()-len;i++){
     boolean a=needle.equals(haystack.substring(i, i+len));
     if(a)
      break;
     }
     if(i<=haystack.length()-len)
     return i;
     return -1;
     
}
}

阅读(490) | 评论(0) | 转发(0) |
0

上一篇:Remove Element

下一篇:DivideTwoIntegers

给主人留下些什么吧!~~