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 ...
随机推荐
- 几段Python小程序
程序片段1 第一个需求是需要生成一些随机的时间,例如需要随机生成从一年前到现在的一些时间,刚开始折腾了半天,最后的代码如下: from datetime import timedelta from d ...
- 学习廖雪峰的Python教程之数据类型
数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定 ...
- spirngMvc
配置方式就略了 直接开始注解方式: 1. 新建项目 2. 导入jar包 3. 创建controller,用注解方式声明 4. 在web.xml配置核心分发器DispatcherServlet ...
- jdk?jre?
很多人都搞不懂什么是jdk,什么是jre,只知道电脑安装了这两个就能开发和运行java程序,这里我简单讲讲什么是jdk,什么是jre. jdk,即Java Development Kit,故名思意就是 ...
- ansible-galera集群部署(13)
一.环境准备 1.各主机配置静态域名解析: [root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain local ...
- 【剑指Offer】10、矩形覆盖
题目描述: 我们可以用2 X 1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2 X 1的小矩形无重叠地覆盖一个2 X n的大矩形,总共有多少种方法? 解题思路: 我们可以以2 X ...
- 洛谷P1012 拼数【字符串+排序】
设有nn个正整数(n≤20)(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3n=3时,33个整数1313,312312,343343联接成的最大整数为:3433121334331 ...
- 与公司2位经理的交流,Web开发知识库建设
1.代码库3种类型 WebCommon:网站开发技术选型和最佳实践 FansCommons :各种可以复用的代码 CentronCore,CentronWeb 3种类型:通用,web,环境(通用+We ...
- phpcms 电脑手机合并
电脑手机端 ========================== <script type="text/javascript"> function browserRed ...
- 学习EXTJS6(8)基本功能-表单的基础表字段Ext.form.field.Basic
Ext.form.field.Basic是表单字段的基类. Ext.form.field.Text Ext.form.field.TextArea Ext.form.field.Number Ext. ...