实现 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() 定义相符。

class Solution {
public int strStr(String haystack, String needle) {
int needleLen = needle.length(); if (needleLen == 0) {
return 0;
} for (int i = 0; i < haystack.length(); i++) {
if ((i + needleLen) <= haystack.length() && haystack.substring(i, i + needleLen).equals(needle)) {
return i;
}
} return -1;
}
}

  

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

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

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

  2. 44. leetcode 28. Implement strStr()

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

  3. 28.实现 strStr() 函数

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

  4. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

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

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

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

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

  7. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

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

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

  9. 【LeetCode】28. 实现 strStr()

    28. 实现 strStr() 知识点:字符串:KMP算法 题目描述 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 ne ...

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

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

随机推荐

  1. CentOS 7 用户及权限管理

    用户及组的管理: 安全上下文: 进程以其发起者的身份运行: 进程对文件的访问权限,取决于发起此进程的用户的权限 系统用户:为了能够让那些后台进程或服务类进程以非管理员的身份运行,通常需要为此创建多个普 ...

  2. zabbix添加自定义监控项目

    在zabbix里添加一个自定义监控项目,简单做个笔记,怕忘了 首先需要定义 zabbix_agentd.conf  中的 UnsafeUserParameters 修改为 UnsafeUserPara ...

  3. phxpaxos实现状态机CAS操作

    看过了phxpaxos的实现,发现选主逻辑中非主也能够调用Propose.因此即使开启了选主功能,也可能会出现两个人同时Propose的场景.而Propose时,InstanceID只是作为输出而非输 ...

  4. Linux开始结束ping命令

    ctrl+c可以终止ping ctrl+z可以暂停ping,该暂停只是把进程放到后台去了,使用命令fg可以调出到前台来 通过以下命令可以设置次数: ping -c    10 (次数)  ip(域名) ...

  5. 利用redis实现分布式锁知识点总结及相关改进

    利用redis实现分布式锁知识点总结及相关改进 先上原文,本文只为总结及对相关内容的质疑并提出若干意见,原文内容更详细https://www.cnblogs.com/linjiqin/p/800383 ...

  6. Python数据分析学习(二):Numpy数组对象基础

    1.1数组对象基础 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bord ...

  7. python 全局变量

    修改全局变量 name = 'jason' def change_name(): global name name = 'Jason'

  8. gerapy 实现自动化部署

    1 安装 2 在需要部署的目录下运行 gerapy init 会在当前目录下生成一个gerapy目录,并在gerapy目录下有一个projects 目录 3 切换到gerapy 目录 cd gerap ...

  9. 19-06 【phpunit和docker】

    phpunit简介 在用PHP做项目的时候,有时候我们需要写一些测试代码,其中可能包含单元测试(比如字符串处理,ip解析,mobile解析等). 我们常用的工具是phpunit,它很方便地帮我们组织测 ...

  10. centos 7.4安装python3.7.4

    转自https://www.cnblogs.com/zhanglong8681/p/8421512.html 1.下载安装包 Linux下默认系统自带python2.7的版本,这个版本被系统很多程序所 ...