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. CF367 E - Working routine

    十字链表模拟 #include<bits/stdc++.h> using namespace std; int n,m,q; struct Node{ int v; int d,r; }m ...

  2. 动态添加弹出页面(shiro项目中来的七)

    一,图解 二,代码 2.0,新增代码 //打开编辑属性(新增) function dialog_open(){ $("#dialog-add").css("display ...

  3. tp5时间戳转日期的方法

    {:date('Y-m-d H:i',$vo['create_time'])}

  4. 描述一下Spring框架的作用和优点?

    Spring框架的作用和优点如下: 1.Spring是一个开源的轻量级的应用开发框架,其目的是用于简化企业级应用程序开发,减少侵入: 2.Spring提供的IOC和AOP应用,可以将组建的耦合度降低至 ...

  5. Mybatis常见面试题

    Mybatis常见面试题 #{}和${}的区别是什么? #{}和${}的区别是什么? 在Mybatis中,有两种占位符 #{}解析传递进来的参数数据 ${}对传递进来的参数原样拼接在SQL中 #{}是 ...

  6. [BZOJ1607] [Usaco2008 Dec] Patting Heads 轻拍牛头 (数学)

    Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号 ...

  7. 20.1章JSON语法

    1,语法 JSON有三种类型的值 简单值:使用与JavaScript相同的语法,可以在JSON中表示字符串,数值,布尔值,null.但是JSON不支持JavaScript中特殊的值undefined. ...

  8. 通过返回动态改变textview和imageview

    //获取并显示优惠券ID Intent intent = getIntent(); awardID=(TextView)findViewById(R.id.awardID); String id = ...

  9. 项目角度谈矢量切片运用以及Geoserver处理自定义规格矢量切片方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1. 背景 矢量切图方案目前已经是很常见的一个方案,在2016年时团队 ...

  10. ubuntu中eclipse 不支持gbk编码问题解决办法

    今天在ubuntu 下, 把Windows下工程导入Linux下Eclipse中,由于工程代码,是GBK编码,而Ubuntu默认不支持GBK编码,所以,要让Ubuntu支持GBK. 方法如下: 1.修 ...