1、题目描述

2.题目分析

字符串操作,注意边界条件即可。

3.代码

  1. int strStr(string haystack, string needle) {
  2. int n = needle.size();
  3. int res = -;
  4. if( needle.size() == )
  5. return ;
  6. for(string::iterator it = haystack.begin() ; it != haystack.end(); ++it){
  7. if((*it) == needle[]){
  8. bool isEqual = true;
  9. bool isEnd = false;
  10. for(int i = ; i < n; i++){
  11. if( it + i == haystack.end() ){
  12. isEnd = true;
  13. break;
  14. }
  15. if( needle[i] != *(it+i)){
  16. isEqual = false;
  17. break;
  18. }
  19. }
  20. if( isEnd == true )
  21. break;
  22. if( isEqual == true){
  23. res = it-haystack.begin();
  24. break;
  25. }
  26.  
  27. }
  28. }
  29. return res;
  30.  
  31. }

LeetCode题解之 Implement strStr()的更多相关文章

  1. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  2. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  3. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

  5. 【LeetCode】28 - Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  7. LeetCode OJ:Implement strStr()(实现子字符串查找)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  9. 【算法】LeetCode算法题-Implement strStr

    这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...

随机推荐

  1. Intellij IDEA 环境配置与使用

    Intellij IDEA 是我感觉最牛X的IDE开发工具,没有之一! 先share一篇教程: http://pan.baidu.com/s/1i3fzJff 调整字体 设置默认的JDK 显示行号 版 ...

  2. 用分布式缓存提升ASP.NET Core性能

    得益于纯净.轻量化并且跨平台支持的特性,ASP.NET Core作为热门Web应用开发框架,其高性能传输和负载均衡的支持已广受青睐.实际上,10-20台Web服务器还是轻松驾驭的.有了多服务器负载的支 ...

  3. Vue图片懒加载插件 - vue lazyload的简单使用

    Vue module for lazyloading images in your applications. Some of goals of this project worth noting i ...

  4. maven - 使用nexus 搭建maven私服

    1, java环境 [wenbronk@localhost nexus]$ java -version java version "1.8.0_121" Java(TM) SE R ...

  5. rpm 软件包管理

    rpm---Redhat Pachage Manager 挂载光盘: [root@localhost sdb1]# mount /dev/sr0 /mnt [root@localhost sdb1]# ...

  6. 制作openstack使用的Ubuntu镜像

    一.环境准备 OS:Ubuntu-14.04 制作镜像版本:Ubuntu-14.04.4-server-amd64.iso 查看是否支持虚拟化(有输出代表支持,否则在BIOS页面中设置即可): egr ...

  7. 下载imagenet2012数据集,以及label说明

    updated@2018-12-07 15:22:08 官方下载地址:http://www.image-net.org/challenges/LSVRC/2012/nonpub-downloads , ...

  8. WebForm 【复合控件】

    一 复合控件(取值,赋值用法相近)  RadioButtonList      --单选按钮 (一组列表)  <asp:RadioButtonList ID="RadioButtonL ...

  9. 浅谈sql中的in与not in,exists与not exists的区别以及性能分析

    1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表 ...

  10. java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.String(Short类型无法强转成String类型)

    有一行Java代码如下: String code1 = (String)qTable1.getValueAt(i, 0); 这是一个Java的图形界面获取表格中值的代码,其中qTable1.getVa ...