题目


Implement strStr().

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

Example 1:

  Input:haystack = "hello", neddle = "ll"

  Output:2

Example 2:

  Input: haystack = "aaaaa", needle = "bba"

  Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

思路


(1)题意

给定一个haystack字符串和一个needle字符串,需要我们做的是在haystack字符串中找出needle字符串出现的第一个位置(从0开始),如果不存在返回-1。此外,如果needle字符串为空,返回0。

(2)如何在在haystack字符串中找出needle字符串?

我们想到将needle字符串视为haystack字符串的一个子串,利用substr(pos, n)函数返回haystack字符串中从pos到pos+n位置的字符串,检验返回的字符串与needle是否一致,如果一致则返回pos,否则返回-1。这里的pos应该是needle字符串第一个字符在haysatck字符串中的位置,n应该是needle字符串的长度。

(3)注意

这里我们要注意的是遍历次数(循环次数),因为是在haystack字符串中找needle字符串,那么循环次数一定不会超过haystack.size() - needle.size(),超过这个次数,说明needle肯定不存在。

Tips


string(C++)

(1)empty

语法:bool empty()

如果字符串为空,返回true;否则返回false。

(2)substr

语法:basic_string substr(size_type index, size_type num = npos)

substr返回字符串的一个子串,从index开始,到index+num结束,子串长度为num。如果没有指定num,则默认值是string::npos,这样substr()返回从index开始的剩余字符串。

C++

class Solution {
public:
int strStr(string haystack, string needle) { //先考虑特殊情况 //特殊情况1:needle字符为空
if(needle.empty())
return 0;
//特殊情况2:needle字符长度大于haystack字符长度或者haystack字符长度为0
if(needle.size() > haystack.size() || haystack.empty() )
return -1; for(int i=0;i<haystack.size() - needle.size() + 1;i++){ if(haystack[i] == needle[0] && haystack.substr(i, needle.size()) == needle )
return i; } return -1;
}
};

Python

总结

  • 代码要尽可能精简
  • 写一个程序时首先要考虑特殊情况
  • 要考虑能否对循环次数作优化以提高程序效率

28. Implement strStr()[E]实现strStr()的更多相关文章

  1. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  2. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  3. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  4. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  5. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

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

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

  7. 28. Implement strStr()

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

  8. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  9. 【LeetCode】28 - Implement strStr()

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

  10. Java [leetcode 28]Implement strStr()

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

随机推荐

  1. Windows下使用Caffe-Resnet

    参考文章: 编译历程参考:CNN:Windows下编译使用Caffe和Caffe2 caffe的VS版本源代码直接保留了sample里面的shell命令,当然这些shell命令在Windows平台下是 ...

  2. 作业08之《MVC实现用户权限》

    1. 赋给用户一个userid,在用户角色表将用户和角色关联起来,在角色权限表中将角色和权限对应起来,权限表中存储的是左边菜单栏的名称. 2. 在判断权限时,通过用户的userid,获取其角色id,然 ...

  3. jquery-pjax

    项目介绍: Pjax是jQuery的一个插件,Pjax即pushState + Ajax,是实现无刷新Ajax加载并解决浏览器前进和后退问题的一个开源实现. 在2012年8月28日发布0.9版本. P ...

  4. js获取当前具体时间

    /** * 获取当前时间 * @param isTime true:显示日期和时间,如:2018-09-20 13:25:12:false:显示日期,如:2018-09-20. * @returns ...

  5. tomcat映射java目录 sever.xml

    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />        --> & ...

  6. 文献阅读 | Benefits and limitations of genome-wide association studies

    参考:今日阅读:GWAS的优劣势 - Omics Liu  Omics 待续~

  7. spring boot jpa 无法使用findOne

    (findOne(id))说我无法转换成相应的类型,换一种即可,如下: user = userRepository.findOne(id);//spring 2.x用不了 @GetMapping(&q ...

  8. 原生js通过最外层id获取下面指定的子元素

    需求:在vue中使用v-for循环出来的元素,设置动态id,之后获取下面的所有textarea标签 template: <table cellpadding = 2 v-for="(i ...

  9. loadrunner报错总结

    1.报错   没有缓存空间可用   TCP超时释放时间?是解决刚才那个报错的? 解决方法如下  修改TcpTimedWaitDelay值为1和MaxUserPort值为65534.最后,重启!  完美 ...

  10. [Ynoi2014]不归之人与望眼欲穿的人们

    题目大意: 给定一个序列,每次单点修改一个数,或给定$x$,询问最短的or起来大于等于$x$的区间的长度(不存在输出-1). 解题思路: 在太阳西斜的这个世界里,置身天上之森.等这场战争结束之后,不归 ...