题目要求:Implement strStr()

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Java:

    public int strStr(String haystack, String needle) {

        if (haystack == null || needle == null) {
return 0;
} if (needle.length() == 0) {
return 0;
} for (int i = 0; i < haystack.length(); i++) {
if (i + needle.length() > haystack.length()) {
return -1;
} int m = i;
for (int j = 0; j < needle.length(); j++) {
if (needle.charAt(j) == haystack.charAt(m)) {
if (j == needle.length() - 1) {
return i;
}
m++;
} else {
break;
}
}
} return -1; }

LeetCode 028 Implement 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. [LeetCode] 28. Implement strStr() 实现strStr()函数

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

  4. [leetcode 27]Implement strStr()

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

  5. Leetcode #28. Implement strStr()

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

  6. 【leetcode】Implement strStr() (easy)

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

  7. 【leetcode】Implement strStr()

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

  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 ...

随机推荐

  1. 20200725_java爬虫_项目创建及log4j配置

    0. 摘要 0.1 添加log4j依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>sl ...

  2. 基于gin的golang web开发:模型验证

    Gin除了模型绑定还提供了模型验证功能.你可以给字段指定特定的规则标签,如果一个字段用binding:"required"标签修饰,在绑定时该字段的值为空,那么将返回一个错误.开发 ...

  3. PHP date_formate使用相关

    $date=date_create("2016-09-25"); echo date_format($date,"Y/m/d H:i:s");要使用date_f ...

  4. Elasticsearch 第六篇:聚合统计查询

    h2.post_title { background-color: rgba(43, 102, 149, 1); color: rgba(255, 255, 255, 1); font-size: 1 ...

  5. SSM使用Ueditor

    富文本编辑器(UEditor) 1. 下载UEditor富文本编辑器 建议下载 utf8-jsp 版本的,结构目录如下: 下载地址:链接:https://pan.baidu.com/s/1Nq0oJB ...

  6. 在pandas中使用pipe()提升代码可读性

    1 简介 我们在利用pandas开展数据分析时,应尽量避免过于碎片化的组织代码,尤其是创建出过多不必要的中间变量,既浪费了内存,又带来了关于变量命名的麻烦,更不利于整体分析过程代码的可读性,因此以流水 ...

  7. Netty源码解析 -- ChannelPipeline机制与读写过程

    本文继续阅读Netty源码,解析ChannelPipeline事件传播原理,以及Netty读写过程. 源码分析基于Netty 4.1 ChannelPipeline Netty中的ChannelPip ...

  8. 寻找性能更优秀的动态 Getter 和 Setter 方案

    反射获取 PropertyInfo 可以对对象的属性值进行读取或者写入,但是这样性能不好.所以,我们需要更快的方案. 方案说明 就是用表达式编译一个 Action<TObj,TValue> ...

  9. c# 生成xml的结构 专业代码

    详细看代码把 public ArrayList kepingyi(string names="") { ArrayList list = new ArrayList(); stri ...

  10. JavaScript的原型对象prototype、原型属性__proto__、原型链和constructor

    先画上一个关系图: 1. 什么是prototype.__proto__.constructor? var arr = new Array; 1. __proto__是原型属性,对象特有的属性,是对象指 ...