首页 > 其他 > 详细

(LeetCode)Implement strStr()

时间:2015-07-31 18:39:54      阅读:187      评论:0      收藏:0      [点我收藏+]

原题如下:

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)Implement strStr()

原文:http://blog.csdn.net/snchenjt/article/details/47171425

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