#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. 前端(五)之display 总结与浮动

    前端之浮动布局.清浮动 display 总结 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  2. C# 隐式转换关键字 implicit

    implicit 关键字用于声明隐式的用户定义类型转换运算符. 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换. 引用摘自:implicit(C# 参考 ...

  3. [译]如何在.NET Core中使用System.Drawing?

    你大概知道System.Drawing,它是一个执行图形相关任务的流行的API,同时它也不属于.NET Core的一部分.最初是把.NET Core作为云端框架设计的,它不包含非云端相关API.另一方 ...

  4. vs2010 编译平台 X86 X64 anycpu

    X86既32位程序,X64既64位程序,anycpu会根据当前的操作系统位数决定 但是如果应用程序编译成anycpu,会由操作系统位数决定,如果是dll之类的,会由调用dll的主程序位数决定 所以一般 ...

  5. [PHP]算法-堆排序的PHP实现

    1.堆(二叉堆):可以视为一棵完全的二叉树,除了最底层之外,每一层都是满的,这使得堆可以利用数组来表示,每一个结点对应数组中的一个元素 2.给出某个结点的下标,可以计算出父结点的和孩子结点的下标; p ...

  6. 腾讯云下的CentOS7 配置 Apache服务器

    第一步 :安装Apache服务程序(软件包名为httpd) * yum install httpd 第二步:配置httpd.conf文件 * vi /etc/httpd/conf/httpd.conf ...

  7. MyBatis中映射器Mapper概述

    MyBatis真正强大之处在于它的映射器.因为它异常强大并且编写相对简单,不仅比传统编写SQL语句做的更好并且能节省将近95%的代码量 XML中顶级元素汇总 cache: 给定命名空间的缓存配置 ca ...

  8. 推荐好用的JavaScript模块

    译者按: 作者将自己常用的JavaScript模块分享给大家. 原文:

  9. laravel 数据库操作(表、字段)

    1)创建表(make:migration create),例如创建 articles php artisan make:migration create_articles_table --create ...

  10. Spider-five

    一.Scrapy框架 1. Scrapy框架主要组成 a. Scrapy三个对象: request请求对象.response响应对象.item数据对象(字典) b. Scrapy五个核心组件: Spi ...