(LeetCode)Implement strStr()
javaleetcode
原题如下:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
我的代码:
public class Solution {public int strStr(String haystack, String needle) {if(needle.length()==0){return 0;}if(haystack.length()<needle.length()){return -1;}boolean flag = false;int mark = -1;for(int i =0 ;i<=haystack.length()-needle.length();i++){if(needle.charAt(0)==haystack.charAt(i)){if(haystack.substring(i, i+needle.length()).equals(needle)){flag = true;mark = i;break;}}}return flag ? mark:-1;}}
版权声明:本文为博主原创文章,未经博主允许不得转载。
上一篇(LeetCode)杨辉三角形Pascal's Triangle
顶0踩0
,黑夜下,撕开那张面具尽是怠倦的容颜,