28. Implement strStr()

来源:互联网 发布:学python工作后累不累 编辑:程序博客网 时间:2024/06/06 01:11

28. Implement strStr()

  • 题目描述:Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

  • Example 1:

    Input: haystack = "hello", needle = "ll"Output: 2
  • Example 2:

    Input: haystack = "aaaaa", needle = "bba"Output: -1
  • 代码:

    package String;/*** @Author OovEver* @Date 2017/12/6 10:55*/public class LeetCode28 {  public int strStr(String haystack, String needle) {      for(int i=0;;i++) {          for(int j=0;;j++) {              if (j == needle.length()) {                  return i;              }              if (i + j == haystack.length()) {                  return -1;              }              if (needle.charAt(j) != haystack.charAt(i + j)) {                  break;              }          }      }  }}
原创粉丝点击