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 ...
随机推荐
- Java 第六章
第六章 for语法:for(表达式①;表达式②;表达式③){ //④循环操作}表达式含义:表达式1:赋值语句, 它用来给循环变量赋初值 例如:int i = 1;表达式2:循环条件,一个关系表达式, ...
- C#的动态链接库和XML配置
今天对昨天的实例进行了改进和提高,将堆排序和冒泡排序封装在一个动态链接库中,提供函数调用和事件委托.此外加入XML进行配置,在XML中存入相关配置信息,提供一个单独的XmlClass对其进行操作,加深 ...
- The Moving Points HDU - 4717
There are N points in total. Every point moves in certain direction and certain speed. We want to kn ...
- 异常-----Template user.ftl not found
freemarker 1.错误描述 java.io.FileNotFoundException: Template user.ftl not found. at freemarker.template ...
- WPF基础篇之空间布局
由于之前自己做的都是大多是B/S架构的项目,加入新公司,公司现在用的WPF,在WPF中一个比较重要的知识点:布局 在网上找到一篇比较好的介绍WPF布局的文章. 文章地址:http://www.cnbl ...
- 【Luogu1273】有线电视网(动态规划)
[Luogu1273]有线电视网(动态规划) 题面 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端, ...
- 【BZOJ2190】仪仗队(数论)
[BZOJ2190]仪仗队(数论) 题面 粘链接,题目中有图片 题解 对于题意,可以考虑 如果有\((i,j)\)能够被看见 那么,\((ki,kj)\)就一定不能看见 所以,如果一个点能够被看见,则 ...
- Django入门-基本数据库API
# 现在系统里还没有 Question 对象 >>> Question.objects.all() <QuerySet []> # 创建新 Question # 在 se ...
- .net remoting在wpf中的应用
我做一个remotting的通讯测试,让控制台程序和wpf窗体通讯.具体实现的功能如下: 1.wpf获取信息在控制台上显示 2.控制台启动wpf,以及在屏幕前端显示 首先,我们来看项目结构: 共三个项 ...
- HTTP架构介绍(1) Web服务器和代理服务器
HTTP应用协议本身是不能运行的,它需是需要架构在硬件和软件解决方案上,才能在万维网上提供高效的传输服务. 在这系列的文章中,我们将会了解到以下概念: Web服务器 代理服务器 缓存 网关.信道和中继 ...