LeetCode3.2 @ Implement strStr() D4F5

来源:互联网 发布:综合管线设计软件 编辑:程序博客网 时间:2024/05/29 16:52

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

Wiki:A Needle in a haystack is a figure of speech used to refer to something that is difficult to locate in a much larger space.

来源于http://blog.csdn.net/linhuanmars/article/details/20276833

注意总结::

【模型】两个字符串或数组,注意for循环的写法

public class Solution {    public String strStr(String haystack, String needle) {          if(haystack==null || needle==null || needle.length()==0)            return haystack;        if(haystack.length()<needle.length())            return null;        for(int i=0;i<=haystack.length()-needle.length();i++){            boolean successFlag=true;            for(int j=0;j<needle.length();j++){                if(haystack.charAt(i+j)!=needle.charAt(j)){                    successFlag=false;                    break;                }            }            if(successFlag)                return haystack.substring(i);        }        return null;    }  }



0 0