首页 > 其他 > 详细

【Leetcode】Implement strStr()

时间:2016-06-06 01:08:12      阅读:170      评论: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.

算法

[java] view plain copy
 技术分享技术分享
  1. public int strStr(String haystack, String needle) {  
  2.     char h[] = haystack.toCharArray();  
  3.     char n[] = needle.toCharArray();  
  4.     int i = 0, j = 0;  
  5.     while (i < h.length && j < n.length) {  
  6.         if (h[i] == n[j]) {  
  7.             j++;  
  8.             i++;  
  9.         } else {  
  10.             i = i - j + 1;  
  11.             j = 0;  
  12.         }  
  13.   
  14.     }  
  15.     if (j == n.length) {  
  16.         return i - needle.length();  
  17.     } else {  
  18.         return -1;  
  19.     }  
  20. }  

【Leetcode】Implement strStr()

原文:http://blog.csdn.net/yeqiuzs/article/details/51590930

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