#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. [转] 使用slim3快速开发RESTful API

    本文转自:https://blog.csdn.net/u011250882/article/details/50101599 版权声明:本文为博主原创文章,转载请注明出处和作者名,尊重别人也是尊重自己 ...

  2. 结构型---代理模式(Proxy Pattern)

    代理模式的详细介绍 代理模式按照使用目的可以分为以下几种: 远程(Remote)代理:为一个位于不同的地址空间的对象提供一个局域代表对象.这个不同的地址空间可以是本电脑中,也可以在另一台电脑中.最典型 ...

  3. mybatis_10关联查询_一对多

    在使用mybatis框架的时候,很多时候需要一个查询结果里的属性包含多个对象,即一条查询结果有属性是集合,这个时候就需要使用collection标签 模型里面有集合 案例: 第一步:在Orders中添 ...

  4. Spring Boot 2.0 升级指南

    Spring Boot 2.0 升级指南 前言 Spring Boot已经发布2.0有5个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把项目中Spring Boot 版本做了升级,顺便整理下 ...

  5. 老王带你走过 Kafka 入门教程

    Apache Kafka是分布式发布-订阅消息系统,在 kafka官网上对 kafka 的定义:一个分布式发布-订阅消息传递系统. 它最初由LinkedIn公司开发. Linkedin于2010年贡献 ...

  6. js循环json得到 键和值

    var jsondata=[{"男":4,"女":3,"不详":0},{"男one":23,"女two&quo ...

  7. 关于openSetting通过tap的调用

    问题模块 框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本 API和组件 小程序 Bug openSetting 工具 6.7.2 2.3.0 - 当前 Bug 的表现(可附上截图 ...

  8. Html5 和 CSS的简单应用

    本文是利用几个简单的小例子,来实现html+css的简单应用. 菱形链接菜单 本例是采用html5+css3.0设置的菜单链接.其中主要用到了以下几个方面: CSS3.0中的2D变换,如:旋转tran ...

  9. (C#)IP地址与数字地址相互转换

    站长网IP查询地址:http://tool.chinaz.com/ip/ 和ip地址转换为数字的工具地址:http://www.msxindl.com/tools/ip/ip_num.asp 可以看到 ...

  10. Kotlin入门(26)数据库ManagedSQLiteOpenHelper

    共享参数毕竟只能存储简单的键值对数据,如果需要存取更复杂的关系型数据,就要用到数据库SQLite了.尽管SQLite只是手机上的轻量级数据库,但它麻雀虽小.五脏俱全,与Oracle一样存在数据库的创建 ...