题意

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1

样例

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符

算法

  1. 如果haystack字符串为空,直接返回0;

  2. 若needle长度大于haystack,返回-1;

  3. 遍历给定haystack字符串,如果存在元素与needle字符串的首字符相同,继续;否则,返回 -1;

  4. 比较后续字符串,若都相同,返回当前索引值。(可以使用string.substr(index1, len))来实现

code

 class Solution {
public:
int strStr(string haystack, string needle) {
if(needle == "")
return ;
int ans = -;
int i, j;
bool flag;
for(i=; i<haystack.length(); i++)
{
if(haystack[i] == needle[])
{
flag = true;
for(j=; j<needle.length(); j++)
{
if(haystack[i+j] != needle[j])
{
flag = false;
break;
}
}
if(flag == true)
{
ans = i;
break;
}
}
} return ans;
}
};

后来更新的

leetcode-28.实现strStr()的更多相关文章

  1. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  2. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  3. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  4. <每日 1 OJ> -LeetCode 28. 实现 strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  5. [LeetCode] 28. Implement strStr() 实现strStr()函数

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

  6. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  7. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  8. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  9. [LeetCode] 28. Implement strStr() 解题思路

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

  10. Leetcode 28——Implement strStr()

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

随机推荐

  1. java基础-3

    java基础-3 API ​ Application Programming Interfaces --- 应用程序接口 Object 顶级父类 Bin --- 二进制 Oct --- 八进制 Dec ...

  2. H5在WebView上开发小结

    背景 来自我司业务方要求,需开发一款APP.但由于时间限制,只能采取套壳app方式,即原生app内嵌webview展示前端页面.本文主要记述JavaScript与原生app间通信,以及内嵌webvie ...

  3. 小程序开发--移动端分辨率与rpx

    首先说一个很有意思的问题:一块720p的屏幕和1080p的屏幕那个大? 这个问题很有代表性,如果手机竖着放,720p=720px*1280px,而1080p=1080px*1920px;那么在宽度上, ...

  4. MySql 踩坑小记

    MySql 踩坑一时爽,一直踩啊一直爽...   以下记录刚踩的三个坑,emmm... 首先是远程机子上创建表错误(踩第一个坑),于是将本地机器 MySql 版本回退至和远程一致(踩第二个坑),最后在 ...

  5. 遇到的一些Jquery,js函数

     jQuery.extend()        jQuery.merge():函数用于合并两个数组内容到第一个数组. <script> $(function () { ,,], [,,] ...

  6. 【原创】为什么浮点数1e38f + 1 - 1e38f等于0

    1. 问题 为什么1e38f + 1 - 1e38f为0? 2. 分析 ; //00 00 00 02 int *pii = &ii; float i = 1e38f; //7e 96 76 ...

  7. 菜鸟系列docker——docker镜像中(4)

    docker镜像命令 该章节主要介绍docker image相关命令实践操作,建议都一起跑一边 1. docker images 列举本机docker镜像 [centos@jiliguo docker ...

  8. Python系列:一、Python概述与环境安装--技术流ken

    Python简介 Python是一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项 ...

  9. 第一次:lesson eighty seven。

    原文: A car crash. A:Is my car ready yet? B:I don't know sir,what's the number of your car? A:It's LFZ ...

  10. FastReport.Net

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...