Implement strStr().

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

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1
拿到题目,返回下标???直接String.indexOf()就可以了啊,于是就试了一下,于是就过了?还99.9%?看了一下别人的,题目的用意应该是想用比较的方法来实现这个indexOf方法。
strStr这个方法是c++里面的,所以一看到这个题目以为只要返回就好了,对于java来说应该改成implement String.indexOf()方法。还是上代码吧。
public static int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}

别人的:

public int strStr(String haystack, String needle) {
for (int i = 0; ; i++) {
for (int j = 0; ; j++) {
if (j == needle.length()) return i;
if (i + j == haystack.length()) return -1;
if (needle.charAt(j) != haystack.charAt(i + j)) break;
}
}
}

Leetcode 28——Implement strStr()的更多相关文章

  1. 44. leetcode 28. Implement strStr()

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

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

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

  3. Leetcode #28. Implement strStr()

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

  4. Java [leetcode 28]Implement strStr()

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

  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()实现strStr()

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

  7. [LeetCode] 28. Implement strStr() ☆

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

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

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

  9. LeetCode——28. Implement strStr()

    题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...

随机推荐

  1. tcp_wrapper

    介绍 对基于tcp协议开发并提供服务的应用程序,所提供的一层访问控制工具 基于库调用实现其功能 * 库名:libwrap 判断服务是否能够由tcp_wrapper进行访问控制 1. 动态编译 ldd命 ...

  2. studio设置File Templates

    从项目的整体风格考虑,对所有类要进行必要的说明,就注释说明来说首先需要说明是作者,文件创建时间,业务功能说明,这几项是基本的内容,而添加这些说明内容以前可能手动的添加文件标题头,这种做法现在都非常过时 ...

  3. Windows Developer Day Review

    北京时间 3 月 8 日凌晨 1 点钟,今年的第一次 Windows Developer Day 正式召开.    因为时间太晚看不了直播,我也是第二天早上在公司看的重播.整个会议过程有很多值得去研究 ...

  4. Acperience HDU - 5734

    Deep neural networks (DNN) have shown significant improvements in several application domains includ ...

  5. 页面jquery调试的一个宝贵经验(类似于Eclipse中的写出一个对象点它的方法时候用alt加/可以跳出来它所有的方法)

    案例讲解 一,html片段 <div class="page-upload"> <div class="tab-wrapper2"> & ...

  6. ls -l 显示年份

    默认情况下ls -l命令只显示月和日,不显示年份: $ ls -llrwxrwxrwx 1 root root 7 Feb 27 16:12 bin -> usr/bin 如要要显示年份,就要使 ...

  7. Aspose实现Office转PDF (ASP.NET)

    0.添加Aspose的DLL 1.可以直接去官网下载,不过默认是带水印的,如需去除水印可以购买 2.当然也可以在国内的一些下载站下载 3.将Aspose.Cells.dll.Aspose.Words. ...

  8. PyTorch官方中文文档:torch.Tensor

    torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...

  9. 招聘面试—关于Mysql的一点儿总结

    最近半年,作为部门的面试官之一,参加了许多次招聘面试.数据库知识,尤其是对数据的增删改查等操作是软件测试人员的基本功,是面试过程中的必考项.在这其中,有一道题,是我每次面试的必考题. 题目 以Mysq ...

  10. Stanford CS224N 第二课: word2vec详细介绍

    word2vec模型 word2vec 模型有两种: Continuous Skip-gram Model 和 Continuous Bag of Words Model (CBOW). 本文主要讲解 ...