首页 > 其他 > 详细

Leetcode#28Implement strStr()

时间:2015-04-30 01:11:32      阅读:193      评论:0      收藏:0      [点我收藏+]

Implement strStr().

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

分析,找字符串haystack中最先出现字符串needle的位置,需要注意的是字符串可能为""的情况的处理


public class Solution {

    public int strStr(String haystack, String needle) {

        if(needle.equals(""))

            return 0;

        int hl=haystack.length();

        int nl=needle.length();

        //if(hl==nl)

        int j=0;

        int i=0;

        for(i=0;i<hl-nl+1;i++)

        {

            for(j=0;j<nl;j++)

                if(haystack.charAt(i+j)!=needle.charAt(j))

                    break;

            if(j==nl)

                return i;

        }

        return -1;

    }

}


Leetcode#28Implement strStr()

原文:http://7061299.blog.51cto.com/7051299/1640462

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