1、题目描述

2.题目分析

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

3.代码

  int strStr(string haystack, string needle) {
int n = needle.size();
int res = -;
if( needle.size() == )
return ;
for(string::iterator it = haystack.begin() ; it != haystack.end(); ++it){
if((*it) == needle[]){
bool isEqual = true;
bool isEnd = false;
for(int i = ; i < n; i++){
if( it + i == haystack.end() ){
isEnd = true;
break;
}
if( needle[i] != *(it+i)){
isEqual = false;
break;
}
}
if( isEnd == true )
break;
if( isEqual == true){
res = it-haystack.begin();
break;
} }
}
return res; }

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. C++学习知识点

    所谓的学习,知识只是一部分,理解知识是如何被抽象和提炼的过程,才是更重要的 1.c++代码里面的\(反斜杠) C语言的宏要求只能在同一行,是不能跨行的.这里的反斜杠就是告诉编译器,我这里虽然换行了,但 ...

  2. C语言使用hiredis访问redis

    Hiredis 是Redis数据库的简约C客户端库.它是简约的,因为它只是增加了对协议的最小支持,但是同时它使用了一个高级别的 printf-like API,所以对于习惯了 printf 风格的C编 ...

  3. Java中的构造器与垃圾回收

    构造器 在我们初始化对象时,如果希望设置一些默认值,那么就可以使用构造器,在Java中,构造器使用和类同名的名字且没有返回值,如下 class Test{ private String name; T ...

  4. 解决wamp 3.0.6 访问路径出现 403 错误

    <VirtualHost *:80> ServerName localhost DocumentRoot e:/wamp64/www #your local dir path <Di ...

  5. 核心组件之SecurityContextHolder

    作用:保留系统当前的安全上下文细节,其中就包括当前使用系统的用户的信息.   上下文细节怎么表示?     用SecurityContext对象来表示   每个用户都会有它的上下文,那这个Securi ...

  6. GCD之同步异步

    博客地址:http://blog.csdn.net/chaoyuan899/article/details/12554603

  7. SQL SERVER中LIKE使用变量类型输出结果不同

    前言:Sql Server中LIKE里面使用不同的变量类型导致查询结果不一致的问题,其实看似有点让人不解的现象背后实质跟数据类型的实现有关. 一.我们先来创建示例演示具体操作 CREATE TABLE ...

  8. JS存储cookie读取cookie删除cookie详细用法

    假设有这样一种情况,在某个用例流程中,由A页面跳至B页面,若在A页面中采用JS用变量temp保存了某一变量的值,在B页面的时候,同样需要使用JS来引用temp的变量值,对于JS中的全局变量或者静态变量 ...

  9. 反射实现Model修改前后的内容对比

    在开发过程中,我们会遇到这样一个问题,编辑了一个对象之后,我们想要把这个对象修改了哪些内容保存下来,以便将来查看和追责. 首先我们要创建一个User类 public class User { priv ...

  10. Git使用(一)——Cygwin

    1.下载2.安装镜像:1)上海交大的FTP:ftp://ftp.sjtu.edu.cn/sites/cygwin.com/pub/cygwin/2)163的镜像:http://mirrors.163. ...