028实现strStr()
#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()的更多相关文章
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 028 Implement strStr() 实现 strStr()
实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...
- LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
- 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. ...
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- strstr 函数的实现
strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)
应用场景 之前我们已经通过<Spring Cloud Stream消费失败后的处理策略(一):自动重试>一文介绍了Spring Cloud Stream默认的消息重试功能.本文将介绍Rab ...
- c#中的特性Attribute
一:特性是什么?特性怎么创建怎么使用? 这一章节,我想谈谈c#的特性方面的知识,特性大家在工作开发中都很熟悉,比如我们经常见到的 1:key Display --EF 2:Import 3:HttpG ...
- C# 反射Reflection Assembly
反射反射程序员的快乐 一:什么叫反射 反射:是.net framework提供的一个访问metadata的帮助类,可以获取信息并且使用 反射的优点:动态 反射的缺点:1:稍微麻烦 2:能避开编译器的 ...
- 基础篇:8.如何定义变量?js变量有什么特点?
书接上文,废话不多说,直接进入正题,下面我们一起来讨论js中的变量那些事! 那什么是变量? 变量是存储信息的容器,可以存储任何类型的数据. 如何定义变量呢? 变量可以使用短名称,如x,y:也可以是长名 ...
- WPF 获取DataGrid 控件选中的单元格信息
获取 DataGrid 选中的单元格的信息DataGridCellInfo cell_Info = this.studentTable.SelectedCells[0]; studentTableIt ...
- 《Office 365开发入门指南教程》正式上线,限时优惠和邀请分享推广
我很高兴地通知大家,<Office 365 开发入门指南教程>已经正式在网易云课堂上线,你可以通过直接访问 https://aka.ms/office365devlesson 这个短地址 ...
- SQL 用于各种数据库的数据类型(转载) sqlserver 数据类型 取值范围 长度
SQL 用于各种数据库的数据类型 来源 http://www.runoob.com/sql/sql-datatypes.html 面向数据库编程中,数据类型的取值范围.长度,可能是需要经常查看的 ...
- SpringBoot数据库集成-Mybatis
一.java web开发环境搭建 网上有很多教程,参考教程:http://www.cnblogs.com/Leo_wl/p/4752875.html 二.Spring boot搭建 1.Intelli ...
- MapperFacade自动导入失败
MapperFacade自动导入失败 添加以下代码并且保证项目可以扫描到: @Configuration public class OrikaConfig { @Bean public MapperF ...
- .NET 发送电子邮件
static void Main(string[] args) { ///先引入 using System.Net.Mail; ///发送邮件 using (MailMessage mailMessa ...