Leetcode 28——Implement strStr()
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()的更多相关文章
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode]28. Implement strStr()实现strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- LeetCode——28. Implement strStr()
题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...
随机推荐
- box-sizing -- 盒模型
项目开发中,在浏览同事的代码,发现他经常用一个属性--box-sizing,很好奇是什么,于是乎,上网查阅资料学了起来. 首先我们先复习一下盒模型的组成:一个div通常由 content(内容)+ma ...
- lvs-nat 模式-piranha
系统: redhat 6.5 mini 机器名 ip vip gw lvs01(主lvs) 192.168.10.10(外网) 192.168.20.10(内网) 192.168.10.254 lvs ...
- 第十篇:Map/Reduce 工作机制分析 - 数据的流向分析
前言 在MapReduce程序中,待处理的数据最开始是放在HDFS上的,这点无异议. 接下来,数据被会被送往一个个Map节点中去,这也无异议. 下面问题来了:数据在被Map节点处理完后,再何去何从呢? ...
- FFT\NTT总结
学了好久,终于基本弄明白了 推荐两个博客: 戳我 戳我 再推荐几本书: <ACM/ICPC算法基础训练教程> <组合数学>(清华大学出版社) <高中数学选修> 预备 ...
- HttpSessionActivationListener序列化与反序列化
一.序列化与反序列化 1.什么是序列化 把对象转化位字节序列的过程称为序列化(保存到硬盘,持久化) 把字节序列转化位对象的过程称为反序列化(存放于内存) 2.序列化的用途 把对象的字节序列永久保存到硬 ...
- c#多线程同步之EventWaitHandle的应用
最近在研究前辈写的winform代码,其中有一个功能,前辈用了EventWaitHandle.初读代码,有点不理解,慢慢想来,还是可以理解的.这个功能,就是执行某项比较耗时的任务,需要打开旋转图标,等 ...
- IE浏览器右键菜单插件开发(上篇)——自定义一个IE右键菜单项
要做一个IE右键浏览器插件,得3步走. 第一,在IE右键菜单上添加自定义菜单名称,是通过注册表实现的,如下: string regkey = @"Software\Microsoft\Int ...
- 题目1032:ZOJ
题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出. 输入: 题目包含多组用例,每组用例占一行,包含ZOJ三个 ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
- 【Unity与23种设计模式】责任链模式(Chain of Responsibility)
GoF中定义: "让一群对象都有机会来处理一项请求,以减少请求发送者与接收者之间的耦合度.将所有的接受对象串联起来,让请求沿着串接传递,直到有一个对象可以处理为止." 举个现实中的 ...