#pragma once
#include "000库函数.h" /*********************自解**************/
//使用算法中的find 12ms
class Solution {
public:
int strStr(string haystack, string needle) {
if (haystack.size() == && needle.size() != )return -;
if (needle.size() == )return ;
return haystack.find(needle);
}
}; /********************博客解法*****************/
//使用暴力遍寻法 44ms
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return ;
int m = haystack.size(), n = needle.size();
if (m < n) return -;
for (int i = ; i <= m - n; ++i) {
int j = ;
for (j = ; j < n; ++j) {
if (haystack[i + j] != needle[j]) break;
}
if (j == n) return i;
}
return -;
}
}; void T028() {
string haystack, needle;
haystack = "hello";
needle = "ll";
Solution s;
cout << s.strStr(haystack, needle) << endl;
haystack = "aaaaa";
needle = "bba";
cout << s.strStr(haystack, needle) << endl;
}

028实现strStr()的更多相关文章

  1. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. 028 Implement strStr() 实现 strStr()

    实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...

  4. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

  7. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  9. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. JS_object添加变量属性_动态属性

    总结,给对象动态添加变量属性的方法如下: obj[变量]=变量值; 备注: obj.属性=属性值 ; obj={属性:属性值}; 这两种方式添加的属性都不能使用变量作为属性. 犯过的错误: var t ...

  2. C#泛型。

    作用: 使用泛型可以实现算法重用. class Program { static void Main(string[] args) { MyClass<string> myClass = ...

  3. python_字符串的操作

    一:字符串的方法与操作 *注意:首字母为l的为从左边操作,为r的方法为从右边操作 1.__contains__()判断是否包含 判断指定字符或字符串是否包含在一个字符串内,返回值为true或者fals ...

  4. C#设计模式之六适配器模式(Adapter Pattern)【结构型】

    一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题,那[ ...

  5. virtualbox中 Kali Linux安装增强功能

    1. 将VBoxLinuxAdditions.run文件copy出来 2. 赋予执行权限 chmod +x VBoxLinuxAdditions.run 3. 安装 ./VBoxLinuxAdditi ...

  6. Android Lifecycle使用

    引言 Lifecycle 是官方提供的架构组件之一,目前已经是稳定版本,Lifecycle 组件包括LifecycleOwner.LifecycleObserver.Lifecycle 组件是执行操作 ...

  7. js正则表达式之人民币匹配

    人民币格式匹配 小写格式:¥ 符号 和 整数值 与小数3部分组成. (0)代码与运行结果 { // 匹配人民币 let [reg, info, rmb, result] = [ /^(¥)(-?[0- ...

  8. phpstudy 产生You don't have permission to access / on this server.解决

    phpstudy配置好访问目录时候有时候会产生You don't have permission to access / on this server. 解决办法: 修改服务器httpd.conf配置 ...

  9. js 每到达5次换一行

    function getYourString(s) { var res = ''; var length = s.length; for (var i = 0, j = 1; i < lengt ...

  10. [HTML/CSS]三角形

    CSS盒子模型 当我们把padding和width,height全部设置为0,border设为一个较大的像素时 即:我们需要什么方向的三角形,只需把其余的三角形背景色设置为transparent: