题目


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. 【Oracle】truncate分区表

    分区表是生产中常用的一种表,它可以实现数据的按类存放,极大的提高了数据的查询及维护.当我们不需要某一分区的数据时,可以采用truncate来清空分区.实验如下: SQL)) partition by ...

  2. ORACLE锁表解锁

    SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s W ...

  3. Deutsch lernen (01)

    Was macht Martin? - Um 8.00 Uhr steht martin auf. aufstehen - aufstand - ist aufgestanden 起床 Um 6 Uh ...

  4. Java_Web之俱乐部会员信息管理系统

    使用 Jsp实现俱乐部会员信息管理功能,orac1e11g作为后台数据库,该系统包括查看俱乐部会员信息列表和修改俱乐部会员信息两人功能,具体耍求如下: 打开俱乐部会员信息列表页面,以列表方式显示所有俱 ...

  5. JAVA软件工程师应该具备的技能有哪些?

    前言:有朋友问我:学历和能力哪个重要?我个人觉得能力大于学历,没有能力哪来的学历,学历只是证明能力的一方面.为此在能力方面畅谈java软件工程师必备的能力.作为一名合格的java工程师,不仅需要学历, ...

  6. HDU_1398_母函数

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. apicloud图片上传

    app中的图片上传,例如:个人信息页面,上传头像 使用: UIMediaScanner 地址: https://docs.apicloud.com/Client-API/UI-Layout/UIMed ...

  8. 通过yum仓库安装mysql

    1,下载安装包wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm2,安装mysql源yum -y ins ...

  9. Xshell连接linux服务器不成功的乌龙问题

    一般xshell连接linux服务器不成功有以下几个问题: linux防火墙拦截,导致Xshell不能访问linux 操作方法: firewalld(CentOS7):启动 :systemctl st ...

  10. spring-boot启动自动执行sql文件失效 解决办法

    在springboot1.5及以前的版本,要执行sql文件只需在applicaion文件里指定sql文件的位置即可.但是到了springboot2.x版本, 如果只是这样做的话springboot不会 ...