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 ...
随机推荐
- 【Leetcode】【Easy】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- php导出cvs xls xlsx
有两种方法,一种是更改输出头部,一种是使用phpexcel类,很显然前者更方便,下面给出一个demo方法导出cvs/** * 导出日志 */public function excel() { setl ...
- nginx-rtmp加入权限验证的简单方法
nginx-rtmp-module默认不限制推流权限.播放权限.如果想加入权限验证,有很多种方法. 方法一:修改源码如: 如何给 nginx rtmp 服务加入鉴权机制 http://blog.csd ...
- IOS 摇一摇的方法
● 监控摇一摇的方法 ● 方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂) ● 方法2:iOS自带的Shake监控API(非常简单) ● 判断摇一摇的步骤:实现3个摇一摇监听方法 ● ...
- 【LOJ6052】「雅礼集训 2017 Day11」DIV(杜教筛)
点此看题面 大致题意: 求\(1\sim n\)内所有满足\(a>0\)的约数\(a+bi\)的\(a\)之和. 解题思路 首先,我们设\(x=(a+bi)(c+di)(1\le x\le n) ...
- Springmvc+Mybatis+Velocity实现小demo(Maven项目)
转:https://blog.csdn.net/FoolishAndStupid/article/details/52005934 Velocity只是充当一个展示层,和JSP的功能类似,利用myba ...
- Joker Xue
大家好,我是LJ,来自于美丽的魏源故乡——隆回,从小被爸妈带到大,但是现在,我脱离了爸妈的管理,来到了远离家乡的长沙,大学生活当然美好,但是我们在做出每一个决定的同时,可能很少有他们的建议了,不过没有 ...
- Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作
一.SQLite数据库: 在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进 ...
- EF执行SQL语句
使用上下文中的Database.SqlQuery<对应的表名>(sql语句) var data = dbcenter.Database.SqlQuery<CcBusiFormview ...
- TensorFlow安装环境的误区
安装py一定要注意安装的版本,我一开始安装的3.7版本的,现在还没有支持,另外,看清楚自己电脑是32位还是64位的