28. Implement strStr()[E]实现strStr()
题目
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()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn rever
import java.util.ArrayList; import java.util.List; public class AA { public static void main(String[ ...
- 【Oracle】数据迁移工具(1):SQL Loader
SQL Loader是一种数据加载工具,可以把外部数据加载到Oracle数据库中.SQL Loader中的参数有很多,本文只在第一部分中列出常用参数.要想运用SQL Loader工具 ,需要我们编辑一 ...
- swift里 as、as!、as?区别 T.Type与动态类型
as 1.编译器进行类型转换合法性检查:静态 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: shoppingLi ...
- ionic Plugin插件,与原生app端交互,ionic端代码
创建plugins 目录 definitions.ts文件 definitions.ts文件: import {Plugin} from '@capacitor/core/dist/esm/defin ...
- js与Jquery的对比
// document.getElementById("divCommit").style.display="none";// document.g ...
- win10环境下配置django+Apache2.4.38+python3.6项目
1.)Apache-2.4.38-win64-vc15下载地址: https://www.apachelounge.com/download/VC14/ 解压httpd-2.4.38-win64-VC ...
- 51nod1183 编辑距离【动态规划】
编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除 ...
- [luogu2414 NOI2011]阿狸的打字机 (AC自动机)
传送门 Solution 我们知道AC自动机上如果有一点A的fail[A]->B那么B为A的一个后缀 那么我们的问题\((x,y)\)就变为在y中有多少个点直接或间接连向x的终止节点 如果写暴力 ...
- 自己总结的php开发中用到的工具
需要一个编辑器IDE,推荐用phpstorm. IDE安装完了,还要搞个Xdebug,这个很有用,程序断点跟踪调试就靠他了. phpstom平时使用的时候,编辑界面感觉很枯燥的时候,可以换个主题,换主 ...
- WordPress TinyMCE 编辑器增强技巧大全
说到WordPress自带的TinyMCE 编辑器,有些国人总是不太满意.针对这个情况,倡萌已经介绍了一些增强或替代的方法: WordPress编辑器增强插件:TinyMCE Advanced Wor ...