LeetCode OJ:Implement strStr()(实现子字符串查找)
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
额,当然我用的就是暴力搜索来,没用到KMP之类的算法,有时间再来补上辣,代码如下:
class Solution {
public:
int strStr(string haystack, string needle) {
if(!needle.size()) return ;
if(haystack.size() < needle.size()) return -;
for(int i = ; i <= haystack.size() - needle.size(); ++i){
int start = i;
for(int j = ; j < needle.size() && start < haystack.size() && haystack[start] == needle[j]; ++j, ++start){
}
if(start-i == needle.size()) return i;
}
return -;
}
};
LeetCode OJ:Implement strStr()(实现子字符串查找)的更多相关文章
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- 数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找
数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找 Boyer-Moore字符串查找算法 注意,<算法4>上将这个版本的实现称为Broyer-Moore算法,我看了 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode] 21. Implement strStr()
这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了. 先放题目吧: Implement strStr(). Re ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- leetcode(57)- Implement strStr()
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
随机推荐
- 使用sql语句创建 表空间 和用户,以及如何彻底删除表空间和用户,使用命令导出和导出数据库
创建表空间有很多种方式,在安装好oracle 11g 后在网站上 https://localhost:1158/em 手动创建也可以,但是没有sql直接生成方便,下面介绍下如何用sql语句直接生成表空 ...
- 【MSDN_C#】C#版本介绍
C# 1,Visual Studio .NET 2002 首次发布 C# 1.1,Visual Studio .NET 2003 #line 杂注和 xml 文档注释 C# 2,Visual Stud ...
- CF1155D Beautiful Array(动态规划)
做法 \(f_{i,0}\)表示以\(i\)结尾未操作时的最大值 \(f_{i,1}\)表示以\(i\)结尾正在操作时的最大值 \(f_{i,2}\)表示以\(i\)结尾已结束操作时的最大值 Code ...
- Mysql 整理错误
Mysql 启动报PID错误 Starting MySQL..ERROR! The server quit without updating PID file (/usr/local/mysql/da ...
- 20145312 《Java程序设计》第10周学习总结
20145312 <Java程序设计>第10周学习总结 学习总结 一. 什么是网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的 ...
- 20145314郑凯杰 《Java程序设计》第9周学习总结 积极主动敲代码
20145314郑凯杰 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 ①JDBC(Java DataBase Connectivity) 即java数据库连接,是一种用于 ...
- FutureTask的简单用法
package com.fmp.orderManager.util; import java.util.Date;import java.util.concurrent.Callable;import ...
- LeetCode——Is Subsequence
Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
- gTest&gMock learning
在C++中,编写服务后的一种测试方式是使用google的gTest和gMock结合 之前写py,测试方式是将服务挂起,使用工具模拟请求发包,check resp,这样的缺点在于不方便,即使存下了所有的 ...
- UWP C# 调用 C++/CX
创建一个UWP项目 然后创建一个通用C++运行时项目 右键点击C++项目,添加一个C++类 在头文件中定义一个类 #pragma once namespace ImageFactoryRT { pub ...