LeetCode每日一题(28. Find the Index of the First Occurrence in a String)
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = “sadbutsad”, needle = “sad”
Output: 0
Explanation: “sad” occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.
Example 2:
Input: haystack = “leetcode”, needle = “leeto”
Output: -1
Explanation: “leeto” did not occur in “leetcode”, so we return -1.
Constraints:
- 1 <= haystack.length, needle.length <= 104
- haystack and needle consist of only lowercase English characters.
用 Rabin-Karp 算法
impl Solution {pub fn str_str(haystack: String, needle: String) -> i32 {let needle_hash = needle.chars().fold(0, |mut h, c| {h *= 256;h += c as i32;h %= 101;h});let power = (1..needle.len()).fold(1, |mut p, _| {p *= 256;p %= 101;p});let mut haystack_hash = 0;let mut queue = Vec::new();for (i, c) in haystack.chars().enumerate() {if queue.len() == needle.len() {haystack_hash += 101;haystack_hash -= queue.remove(0) as i32 * power % 101;}haystack_hash *= 256;haystack_hash += c as i32;haystack_hash %= 101;queue.push(c);if queue.len() == needle.len() && needle_hash == haystack_hash && haystack[i + 1 - needle.len()..i + 1] == needle[..] {return (i + 1 - needle.len()) as i32;}}-1}
}
LeetCode每日一题(28. Find the Index of the First Occurrence in a String)相关推荐
- Leetcode每日一题:28.implement-strstr(实现strStr())
思路:KMP实现虽然效率高,但较为复杂: 这里索性直接hash,注意int溢出,一定要取余 int strStr(string haystack, string needle) {int len1 = ...
- LeetCode每日一题——904. 水果成篮
LeetCode每日一题系列 题目:904. 水果成篮 难度:普通 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 你正在探访一家农场,农场从左到右种植了一排果树.这些树用一个整 ...
- 【Leetcode 每日一题】514. 自由之路(BFS+优先队列)
Leetcode 每日一题 题目链接:514. 自由之路 难度: 困难 解题思路: 这道题乍一看,可以选择用动态规划或者BFS来求解.本文使用BFS来进行解答.注意到题中有一个最小的到路径.所以我们可 ...
- LeetCode每日一题——927. 三等分
LeetCode每日一题系列 题目:927. 三等分 难度:困难 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 给定一个由 0 和 1 组成的数组 arr ,将数组分成 3 个 ...
- LeetCode每日一题——811. 子域名访问计数
LeetCode每日一题系列 题目:811. 子域名访问计数 难度:普通 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 网站域名 "discuss.leetcode. ...
- LeetCode每日一题——670. 最大交换
LeetCode每日一题系列 题目:670. 最大交换 难度:中等 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 给定一个非负整数,你至多可以交换一次数字中的任意两位.返回你能 ...
- LeetCode每日一题(题1028)
题1028 前言 题目 思路 代码 错误 后记 前言 最近在刷LeetCode每日一题,每次做完之后总能有些收获,所以想着不如每天写个博客记录一下做的题目的解法以及自己写的时候问题出在哪里. 题目 从 ...
- 4月你好,愚人节果然是笨人,【LeetCode每日一题】1006. 笨阶乘
4月你好,愚人节果然是笨人,[LeetCode每日一题]1006. 笨阶乘 今日题目1006题,每日一题微信交流群可以点击右下角:合作转载->联系我,拉你入群. 目前每日一题两个群,每天推送题目 ...
- leetcode每日刷题计划-简单篇day8
leetcode每日刷题计划-简单篇day8 今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solutio ...
最新文章
- 研三学生举报导师强迫学生延期毕业,事件再三反转,学校回应了!
- 关于软件组织培训的几个值得提倡的建议
- 在MYSQL中输入net stop mysql没有反应?
- 组件Refs(操作DOM的2⃣️两种方法)
- js 更改json的 key
- 工业机器人工具中心点标定的意义_如何理解工业机器人的工具中心点
- Linux rsyslog 转存至日志服务器
- 地铁线路图的设计与实现
- 有哪些 Java 源代码看了后让你收获很多,代码思维和能力有较大的提升?...
- 数据--第42课 - 图的遍历
- 无线接入回传一体化关键技术及标准化进展
- ulead gif animator 5.11中文破解版|ulead gif animator绿色中文破解版下载 v5.11
- gma 教程 | 气候气象 | 计算标准化降水指数(SPI)
- 河套学院2018级计算机一级考试,河套学院2018-2019学年本科生就业率
- android11下文件管理,华为文件管理器下载-华为文件管理器 安卓版v10.11.11.301-PC6安卓网...
- 模拟10位QQ账号的生成
- matlab roundn函数_matlab中round函数具体用法
- 【题解】[CQOI2009] 循环赛
- Moodle二次开发(1)-- 微创新
- 微信商家券对接wechatpay-apiv3
热门文章
- 分段函数c语言编程noi,NOI1.4-13分段函数
- 走进VR游戏开发的世界
- 职场聚焦,现在年轻人的成功标准是什么?
- Task 4: Contextual Word Embeddings (附代码)(Stanford CS224N NLP with Deep Learning Winter 2019)
- 每日一坑:qt dragEnterEvent、dragMoveEvent、dropEvent不响应
- 如何编写 C++ 游戏引擎
- windbg输出SXS.DLL的调试信息
- 专业人士使用的 11 种渗透测试工具
- Windows中MySQL8详细安装教程
- 智能实验室-专用链转换 1.5.0.150