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 ...
随机推荐
- CloudFoundry 之 IBMCloud 项目部署java例子
步骤一 通过官网注册IBMCloud账号 https://idaas.iam.ibm.com/idaas/mtfim/sps/authsvc?PolicyId=urn:ibm:security:aut ...
- [android] 采用服务录制电话&服务的生命周期
根据上一节代码里,加入一个录音功能,上传到服务器,就能实现一个录制器 当手机处于通话状态时,开启录音机 获取MediaRecorder对象,通过new出来 调用MediaRecorder对象的setA ...
- No application encryption key has been specified.
环境:php7.1.10laravel5.5出现: 解决:在根目录下执行: php artisan key:generate OK问题解决
- 在html中使用特殊字体
目的:一首诗,要求从右往左读,垂直排列,类似古文 效果图: html内容: <!doctype html><html lang="en"><head& ...
- Android为TV端助力 自定义activity
今天公司有个需要需要自动弹出界面,而dialog又不符合要求,所以自定义的一个activity的样式 首先在androidmainfest.xml上注册你的activity <activity ...
- listview reclyerview上下拉刷新
x写控件挺麻烦的,因为有很多细节要处理好,列表控件使用太频繁了,网上也各种自定义的方法,一般的listview自定义肯定会联想到加个头部,然后监听事件加动画,其实方式很多种,今天记录的方式是另外一种方 ...
- Android 源码编译之旅
目录 前言 背景 安装软件 正文 Mac 分区 移动硬盘分区 Repo 下载源码 编译 源码导入 Android Studio 查看 碰到的问题 Could not find a supported ...
- filter帅选
var ages = [32, 33, 16, 40]; ages= ages.filter(function checkAdult(obj) {//obj表示数组中的每个元素 return obj ...
- 从头开始:详解MVVM、MVVMLight
究竟为什么要学习MVVM? 相信大部分同学在刚开始接触MVVM的时候(包括我自己),心里默默在想这究竟是什么玩意?一个简单的功能要写一大段代码才能完成,在看到MVVM的核心目标: 1.让UI界面与逻辑 ...
- git 入门教程之安装 git
安装 git git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行,根据自身环境选择安装. Linux 系统 linux 系统安装软件大致有两种途径,一种是利用 ...