Leecode刷题之旅-C语言/python-28.实现strstr()
/*
* @lc app=leetcode.cn id=28 lang=c
*
* [28] 实现strStr()
*
* https://leetcode-cn.com/problems/implement-strstr/description/
*
* algorithms
* Easy (37.86%)
* Total Accepted: 38.6K
* Total Submissions: 102K
* Testcase Example: '"hello"\n"ll"'
*
* 实现 strStr() 函数。
*
* 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置
* (从0开始)。如果不存在,则返回 -1。
*
* 示例 1:
*
* 输入: haystack = "hello", needle = "ll"
* 输出: 2
*
*
* 示例 2:
*
* 输入: haystack = "aaaaa", needle = "bba"
* 输出: -1
*
*
* 说明:
*
* 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
*
* 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
*
*/
int strStr(char* haystack, char* needle) { int i, j; int len1 = strlen(haystack); int len2 = strlen(needle); for(i = ; i <= len1 - len2; i++){ for(j = ; j < len2; j++){ if(haystack[i + j] != needle[j]){ break; } } if(j == len2) return i; } return -; }
c语言自然是应用最最著名的kmp(看毛片)算法。
这个算法的理解可以参考:
https://www.cnblogs.com/yjiyjige/p/3263858.html
--------------------------------------------------------------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=28 lang=python3
#
# [28] 实现strStr()
#
# https://leetcode-cn.com/problems/implement-strstr/description/
#
# algorithms
# Easy (37.86%)
# Total Accepted: 38.6K
# Total Submissions: 102K
# Testcase Example: '"hello"\n"ll"'
#
# 实现 strStr() 函数。
#
# 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置
# (从0开始)。如果不存在,则返回 -1。
#
# 示例 1:
#
# 输入: haystack = "hello", needle = "ll"
# 输出: 2
#
#
# 示例 2:
#
# 输入: haystack = "aaaaa", needle = "bba"
# 输出: -1
#
#
# 说明:
#
# 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
#
# 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
#
#
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
l = len(needle)
for i in range(len(haystack)-l+1):
if haystack[i:i+l] == needle:
return i
return -1
python是真的简单,运用切片就能达到想要的目的了。
Leecode刷题之旅-C语言/python-28.实现strstr()的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
随机推荐
- 从产品展示页面谈谈Hybris的特有概念和设计结构
今天这篇文章来自我的同事,SAP成都研究院Hybris开发团队的开发人员Zhang Jonathan(张健).需要特别介绍的是,张健和成都研究院的其他开发同事不同,张健毕业于电子科技大学,读的专业是英 ...
- Android(java)学习笔记4:线程的控制
1. 线程休眠: Java中线程休眠指让正在运行的线程暂停执行一段时间,进入阻塞状态,通过调用Thread类的静态方法sleep得以实现. 当线程调用sleep进入阻塞状态后,在其休眠的时间内,该线程 ...
- 2018.11.19 Struts2中Action类的书写方式
方式1: 方式2: 方式3
- 2018.10.5 hibernate导入约束,在Eclipse的xml文件实现自动提示
打开Java Resources/Libraries/hibernate-core-5.3.1.Final.jar/org.hibernate/hibernate-mapping-3.0.dtd(hi ...
- 两个list相加
>>> a = ['] >>> b = ['] >>> a+b ['] >>> a = [1,2] >>> b ...
- Array GCD CodeForces - 624D (dp,gcd)
大意: 给定序列, 给定常数a,b, 两种操作, (1)任选一个长为$t$的子区间删除(不能全部删除), 花费t*a. (2)任选$t$个元素+1/-1, 花费t*b. 求使整个序列gcd>1的 ...
- springMVC+thymeleaf form表单提交前后台数据传递
后端: @RequestMapping(value = "/add", method=RequestMethod.POST) public String save(@ModelAt ...
- 基础算法之Dijkstra最短路径
核心思想:以起始原点为中心,想外层扩展,知道扩展到重点为止. 设到A点的最短路径上,A点前驱节点为B,则该路径包含到达节点B的最短路径. S集合代表已经探索过的节点,U集合表示未探索过的节点. 时间复 ...
- sed实现路径替换
shell和sed忘得差不多了,现在更喜欢用python notebook,可以自动补充,所以很方便.但是记得以前用过这个的,试了几次不成功,搜了一下,这几个地方给的比较清晰,尤其是路径替换. 以下内 ...
- Openresty最佳案例 | 第6篇:OpenResty连接Mysql
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616698 本文出自方志朋的博客 centos 安装mysl Centos系统下安装my ...