题目

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-strstr
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

两种解法。

第一种:暴力直接求解,时间复杂度是O(n^2)。

第二种:Rabin-Karp算法。

时间复杂度O(m + n)。

代码

代码1

class Solution {
public int strStr(String haystack, String needle) {
if(haystack == null || needle == null) return -1;
for(int i=0; i < haystack.length() - needle.length() + 1; i++){
int j = 0;
for(; j < needle.length(); j++){
if(haystack.charAt(i + j) != needle.charAt(j)) break;
}
if(j == needle.length()) return i;
}
return -1;
}
}

代码2

class Solution {
static final int BASE = 1000000;
public int strStr(String haystack, String needle) {
if(haystack == null || needle == null) return -1;
if(needle.length() == 0) return 0; int m = needle.length();
//31 ^ m
int power = 1;
for (int i = 0; i < m; i++) {
power = (power * 31) % BASE;
} int targetCode = 0;
for (int i = 0; i < m; i++) {
targetCode = (targetCode * 31 + needle.charAt(i)) % BASE;
} int hashCode = 0;
for (int i = 0; i < haystack.length(); i++) {
hashCode = (hashCode * 31 + haystack.charAt(i)) % BASE;
if(i < m - 1){
continue;
} //减去开始的数 abcd - a
if(i >= m) {
hashCode = hashCode - (haystack.charAt(i - m) * power) % BASE;
if(hashCode < 0){
hashCode += BASE;
}
} //双重校验
if(hashCode == targetCode){
if(haystack.substring(i - m + 1, i + 1).equals(needle)){
return i - m + 1;
}
}
} return -1;
}
}

[算法]实现strStr()的更多相关文章

  1. 用KMP算法实现strStr()

    strStr()函数的用途是在一个字符串S中寻找某个字串P第一次出现的位置.并返回其下标,找不到时返回-1.最简单的办法就是找出S全部的子串和P进行比較,然而这种方法比較低效.假设我们从S的下标0和P ...

  2. leecode刷题(17)-- 实现StrStr

    leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...

  3. POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()

    题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total ...

  4. 70. Implement strStr() 与 KMP算法

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  5. 【算法】LeetCode算法题-Implement strStr

    这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...

  6. 【简单算法】18.实现strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  7. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  8. Linux GCC下strstr的实现以及一个简单的Kmp算法的接口

    今天做了一道题,要用判断一个字符串是否是另一个字符串的子串,于是查了一下strstr的实现. 代码如下: char *strstr(const char*s1,const char*s2) { con ...

  9. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

随机推荐

  1. Vue ---- 项目与环境搭建 初始项目结构 Vue生命周期

    目录 1. vue环境搭建 2. Vue项目搭建 pycharm配置并启动vue项目 3 . 认识项目 1. vue项目目录结构 2. 配置文件:vue.config.js 3. main.js 4. ...

  2. Vue项目无法使用局域网IP直接访问的配置方法

    一般使用 vue-cli 下来的项目是可以直接访问局域网 IP 打开的,比如 192.168.1.11:8080 .但是最近公司的一个项目只可以通过 localhost 访问. 需要配置一下,才可直接 ...

  3. MVC模式与Servlet执行流程

    ##Servlet生命周期 五个部分,从加载到卸载,如同人类的出生到死亡 加载:Servlet容器自动处理 初始化:init方法 该方法会在Servlet被加载并实例化后执行 服务:service抽象 ...

  4. 03-EF Core笔记之查询数据

    EF Core使用Linq进行数据查询. 基本查询 微软提供了一百多个示例来演示查询,地址:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb98 ...

  5. CentOS7 安装 Redis 并设置开机启动

    1.下载 https://redis.io/download cd /usr/local/src wget -c http://download.redis.io/releases/redis-3.2 ...

  6. vscode搭建C/C++环境

    windows安装Mingw-w64 Mingw-w64安装 准备工作 创建文件夹Vc_c++ 在Vc_c++文件夹下创建下面两个文件夹 在g++下创建demo.cpp 在gcc下创建demo.c 打 ...

  7. LAMPSecurity: CTF6 Vulnhub Walkthrough

    镜像下载地址: https://www.vulnhub.com/entry/lampsecurity-ctf6,85/ 主机扫描: ╰─ nmap -p- -sV -oA scan 10.10.202 ...

  8. 微信小程序—支付宝身份验证(支付宝小程序)

    查看应用:https://open.alipay.com/platform/keyManage.htm  这里找到您调用接口的应用 支付宝身份验证快速接入:https://docs.open.alip ...

  9. js解决客户端与服务器时间不一致的问题

    引出 最近在写一个项目时,要根据时间进行不同的展示,直接用new Date().getTime()获取当前时间,结果就出问题了.有些用户擅自修改自己的本地时间,导致获取到的时间并不是当前时间,尴尬. ...

  10. Xposed的新打开方式--Xpatch工作流程分析

    1. Xpatch概述 Xpatch是一款利用重打包的方式,使得被处理的Apk启动时自动加载Xposed模块,来实现应用内Hook的工具. 项目地址:https://github.com/WindyS ...