本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41452047

Implement strStr().

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

Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button  to reset your code definition.

这道题很简单,这里就不啰嗦了。下面给出我自己的解题算法和网站上给出公认的性能比较好的算法,显然,我写的算法还不够好,仅仅作为参考,希望对大家有所帮助。

本人的解题代码如下:

public int strStr(String hack, String need) {
	int needlen = need.length();
	int hacklen = hack.length();
	if (needlen == 0)
		return 0;
	if (needlen == 0 && hacklen == 0 || needlen > hacklen)
		return -1;

	for (int i = 0; i < hacklen; i++) {
		int y = needlen;
		int x = i;
		if (y <= hacklen) {
			y = y + i;
			if (y > hacklen)
				return -1;
			String compare = hack.substring(x, y);
			if (need.equals(compare)) {
				return x;
			}
		}
	}
	return -1;
}

网站上公认比较好的解题代码如下:

public int strStr(String haystack, String needle) {
	for (int i = 0;; i++) {
		for (int j = 0;; j++) {
			if (j == needle.length())
				return i;
			if (i + j == haystack.length())
				return -1;
			if (needle.charAt(j) != haystack.charAt(i + j))
				break;
		}
	}
}

Leetcode_28_Implement strStr的更多相关文章

  1. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

  2. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  4. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  7. strstr函数

    原型:char * strstr( char *haystack,  char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...

  8. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  9. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

随机推荐

  1. JQuery 分页显示jquery-pager-1.0.js

    原版是jquery-pager-1.0.js,经过变更修改加上按照项目中的一些需要修改过来. //初始化分页控件 PagerOptions为配置参数 url为要提交的url地址,如果不需要提交则可以为 ...

  2. Go语言-通道类型

    通道(Channel)是Go语言中一种非常独特的数据结构.它可用于在不同Goroutine之间传递类型化的数据,并且是并发安全的.相比之下,我们之前介绍的那些数据类型都不是并发安全的.这一点需要特别注 ...

  3. Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》

    转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...

  4. Bootstrap3 栅格系统-Less mixin 和变量

    除了用于快速布局的预定义栅格类,Bootstrap 还包含了一组 Less 变量和 mixin 用于帮你生成简单.语义化的布局. 变量 通过变量来定义列数.槽(gutter)宽.媒体查询阈值(用于确定 ...

  5. [精简版]snowing snow

    CSS <style> body { background: #eee; } @keyframes mysnow { 0% { bottom: 100%; opacity: 0; } 50 ...

  6. [BBS]搭建开源论坛之JForum安装使用札记

    本文作者:sushengmiyan 本文地址:http://blog.csdn.net/sushengmiyan/article/details/47761303 目录 目录 BBS搭建开源论坛之JF ...

  7. UDP单播和组播使用SO_REUSEADDR 测试结果

    UDP单播通信 一. 预置条件 A.B在同一台机器,网络中存在往A.B所在的机器的8888端口发送单播UDP数据 A:端口复用绑定在端口8888上 B:端口复用绑定在端口8888上操作步骤:(1)先启 ...

  8. EBS客户化迁移SQL

    检查一些作废了的东西是否在程序包中还有用 SELECT t.* FROM ALL_SOURCE T WHERE T.TEXT LIKE '%CUX_AP_OA_OMS_PROGRAM_ELECT%' ...

  9. [tornado]websocket 最简单demo

    想法 前两天想看看django 长轮询或者是websocket的方案,发现都不太好使. tornado很适合做这个工作,于是找了些资料,参照了做了个最简单demo,以便备用. 具体的概念就不说了,to ...

  10. JAVA面向对象-----java面向对象的六大原则

    现在编程的主流语言基本上都是面向对象的.如C#,C++,JAVA.我们在使用时,已经构造了一个个的类.但是往往由于我们在类内部或外部的设计上存在种 种问题,导致尽管是面向对象的语言,却是面向过程的逻辑 ...