1. #pragma once
  2. #include "000库函数.h"
  3.  
  4. /*********************自解**************/
  5. //使用算法中的find 12ms
  6. class Solution {
  7. public:
  8. int strStr(string haystack, string needle) {
  9. if (haystack.size() == && needle.size() != )return -;
  10. if (needle.size() == )return ;
  11. return haystack.find(needle);
  12. }
  13. };
  14.  
  15. /********************博客解法*****************/
  16. //使用暴力遍寻法 44ms
  17. class Solution {
  18. public:
  19. int strStr(string haystack, string needle) {
  20. if (needle.empty()) return ;
  21. int m = haystack.size(), n = needle.size();
  22. if (m < n) return -;
  23. for (int i = ; i <= m - n; ++i) {
  24. int j = ;
  25. for (j = ; j < n; ++j) {
  26. if (haystack[i + j] != needle[j]) break;
  27. }
  28. if (j == n) return i;
  29. }
  30. return -;
  31. }
  32. };
  33.  
  34. void T028() {
  35. string haystack, needle;
  36. haystack = "hello";
  37. needle = "ll";
  38. Solution s;
  39. cout << s.strStr(haystack, needle) << endl;
  40. haystack = "aaaaa";
  41. needle = "bba";
  42. cout << s.strStr(haystack, needle) << endl;
  43. }

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. CloudFoundry 之 IBMCloud 项目部署java例子

    步骤一 通过官网注册IBMCloud账号 https://idaas.iam.ibm.com/idaas/mtfim/sps/authsvc?PolicyId=urn:ibm:security:aut ...

  2. [android] 采用服务录制电话&服务的生命周期

    根据上一节代码里,加入一个录音功能,上传到服务器,就能实现一个录制器 当手机处于通话状态时,开启录音机 获取MediaRecorder对象,通过new出来 调用MediaRecorder对象的setA ...

  3. No application encryption key has been specified.

    环境:php7.1.10laravel5.5出现: 解决:在根目录下执行: php artisan key:generate OK问题解决

  4. 在html中使用特殊字体

    目的:一首诗,要求从右往左读,垂直排列,类似古文 效果图: html内容: <!doctype html><html lang="en"><head& ...

  5. Android为TV端助力 自定义activity

    今天公司有个需要需要自动弹出界面,而dialog又不符合要求,所以自定义的一个activity的样式 首先在androidmainfest.xml上注册你的activity <activity ...

  6. listview reclyerview上下拉刷新

    x写控件挺麻烦的,因为有很多细节要处理好,列表控件使用太频繁了,网上也各种自定义的方法,一般的listview自定义肯定会联想到加个头部,然后监听事件加动画,其实方式很多种,今天记录的方式是另外一种方 ...

  7. Android 源码编译之旅

    目录 前言 背景 安装软件 正文 Mac 分区 移动硬盘分区 Repo 下载源码 编译 源码导入 Android Studio 查看 碰到的问题 Could not find a supported ...

  8. filter帅选

    var ages = [32, 33, 16, 40]; ages= ages.filter(function checkAdult(obj) {//obj表示数组中的每个元素 return obj ...

  9. 从头开始:详解MVVM、MVVMLight

    究竟为什么要学习MVVM? 相信大部分同学在刚开始接触MVVM的时候(包括我自己),心里默默在想这究竟是什么玩意?一个简单的功能要写一大段代码才能完成,在看到MVVM的核心目标: 1.让UI界面与逻辑 ...

  10. git 入门教程之安装 git

    安装 git git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行,根据自身环境选择安装. Linux 系统 linux 系统安装软件大致有两种途径,一种是利用 ...