LeetCode 028 Implement strStr()
题目要求:Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Java:
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null) {
return 0;
}
if (needle.length() == 0) {
return 0;
}
for (int i = 0; i < haystack.length(); i++) {
if (i + needle.length() > haystack.length()) {
return -1;
}
int m = i;
for (int j = 0; j < needle.length(); j++) {
if (needle.charAt(j) == haystack.charAt(m)) {
if (j == needle.length() - 1) {
return i;
}
m++;
} else {
break;
}
}
}
return -1;
}
LeetCode 028 Implement strStr()的更多相关文章
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return 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 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- 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 ...
随机推荐
- Learn day4 函数参数\变量\闭包\递归
1.函数描述 # ### 函数 """ (1)函数的定义: 功能 (包裹一部分代码 实现某一个功能 达成某一个目的) (2)函数特点: 可以反复调用,提高代码的复用性,提 ...
- go-zero 是如何追踪你的请求链路的
go-zero 是如何追踪你的请求链路 微服务架构中,调用链可能很漫长,从 http 到 rpc ,又从 rpc 到 http .而开发者想了解每个环节的调用情况及性能,最佳方案就是 全链路跟踪. 追 ...
- Java获取不到Canal服务器端数据问题汇总(坑人的东西)
情况1:(基本都是这样的问题,) 1.需要修改canal.properties配置 vim conf/canal.properties canal.instance.parser.parallelTh ...
- 【Java GC系列】垃圾收集简介(1)
说明: 在本文中, Garbage Collection 翻译为 "垃圾收集", garbage collector 翻译为 "垃圾收集器"; 一般认为, 垃圾 ...
- java常用类——包装类
八种基本数据类型对应八种包装类和它们的继承关系 基本数据类型 对应的包装类 boolean Boolean byte Byte short Short int Integer long Long ch ...
- Java_包装类
包装类 在实际应用中, 经常需要把基本数据类型转化为对象以便操作. 因此, Java在设计类时, 为每个基本数据类型设计了一个对应的类进行包装, 这样八个和基本数据类型对应的类统称为包装类(Wrapp ...
- 最长公共子串算法(Longest Common Substring)
给两个字符串,求两个字符串的最长子串 (例如:"abc""xyz"的最长子串为空字符串,"abcde"和"bcde"的最 ...
- Jmeter(二十六) - 从入门到精通 - 搭建开源论坛JForum(详解教程)
1.简介 今天这篇文章主要是给大家讲解一下,如何部署测试环境,这里宏哥部署一个开源测论坛,后边的文章中会用到这个论坛,并且也看到童鞋们在群里讨论如何在开发将测试包发给你以后,你如何快速地部署测试环境. ...
- C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现【可运行】
C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现[可运行] #include <stdio.h> #include <stdlib.h> typedef int Key ...
- 关于ubuntu出现的一些问题的解决方法
1. (1)现象: dpkg: 处理软件包 linux-image-4.15.0-36-generic (--configure)时出错: 子进程 已安装 post-installation 脚本 返 ...