码迷,mamicode.com
首页 > 其他好文 > 详细

28. Implement strStr()

时间:2016-10-10 07:44:12      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路:对应检查char是否一样,不一样直接跳出检查下一组。用boolean来判断这组是不是符合,如果符合直接输出。

119/145

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

 

28. Implement strStr()

标签:

原文地址:http://www.cnblogs.com/Machelsky/p/5944465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!