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. HTML5不允许写结束标记的元素

    HTML5不允许写结束标记的元素 1.area 2.base 3.br 4.col 5.command 6.embed 7.img 8.hr 9.keygen 10.link 11.meta 12.p ...

  2. javaWeb之自动发送邮件生日祝福(ServletContextListener监听)

    在看完本随笔仍然不理解的可以看  javaWeb邮箱发送  :里面有具体的邮箱服务器配置 企业在员工生日当天发送邮箱生日祝福: 一般是用监听器完成:  而合适的监听是ServletContextLis ...

  3. java创建自定义类的数组

    今天在学图论的最小生成树,开始一直在想是用邻接矩阵还是关联矩阵来表示图,但是发现这样都会有好多空间浪费.于是我就自定义一个边的类,里面包含了权值,关联的端点1,端点2,和图的表示字母.发现我想创建11 ...

  4. 下拉框的change事件

    6.1,获取下拉框的值(html标签中没有onchange事件的) <script language="javascript"> $(document).ready(f ...

  5. Tomcat下使用C3P0配置JNDI数据源(在项目的META-INF目录下创建context.xml的文件)

    一.C3P0下载 C3P0下载地址:http://sourceforge.net/projects/c3p0/files/?source=navbar 下载完成之后得到一个压缩包

  6. C# 图解教程 第二章 C#编程概述

    C#编程概述 一个简单的C#程序标识符关键字Main:程序的起始点从程序输出文本注释 C#编程概述 一个简单的C#程序 标识符 标识符是一种字符串,用来命名变量.方法.参数和许多后面将要阐述的其他程序 ...

  7. java中垃圾回收机制和引用类型

    在java中JDK1.2版本以后,对象的引用类型分为四种,从高到低依次为:强引用.软引用.弱引用.虚引用. ①强引用的特点:垃圾回收机制绝不会回收它,即使内存不足时,JVM宁愿抛出OutOfMemor ...

  8. jsp学习笔记之:内置对象

    application对象: 设置一个名为name,值为val的应用内共享的数据 <% application.setAttribute("name",val); %> ...

  9. 2018 年 3 月 iOS架构师 面试总结

    序言: 今年2月中下旬因为个人原因,换了一份工作,3月初期间面试了有3,4家,基本都是D轮或者刚刚上市的公司,也有上榜的BAT,也从他们的面试笔试中看到了自己的一些不足,于是就想写出来和大家分享一下, ...

  10. Solidity调试 - 实现变量打印

    Solidity没有print或console.log方法可以用来打印变量,这会给我们调试程序增加难度. Solidity有event功能,可以在event中记录变量信息,通过调用event方法也可以 ...