LeetCode & Q28-Implement strStr-Easy
String Two Pointers
Description:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
字符串匹配问题,效率最高的是用KMP算法,这里用了双重for循环,但是循环条件控制的不好。
我认为这题想AC的关键是测试用例的编写,一定要想全,至少有以下几个:
- 分别是null或"",这两者是有区别的,""是表示有字符串的,只是为空,length() 为0,而null表示没有字符串,不存在length()。共有四种组合。
- 完全匹配
- 不完全匹配:haystack更长;needle更长
my Solution:
public class Solution {
public int strStr(String haystack, String needle) {
if (haystack != null && needle != null) {
if (needle.isEmpty()) {
return 0;
} else {
int j = 0;
int index = 0;
for (int i = 0; i < haystack.length(); i++) {
index = i;
for (j = 0; j < needle.length(); j++) {
if (haystack.charAt(index) != needle.charAt(j)) {
break;
}
index++;
if (index >= haystack.length()) {
j++;
break;
}
}
if (j == needle.length()) {
return i;
}
}
}
}
return -1;
}
}
改进版:
public class Solution {
public int strStr(String haystack, String needle) {
if (haystack != null && needle != null) {
if (needle.isEmpty()) {
return 0;
} else {
for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {
int j = 0;
for (; j < needle.length(); j++) {
if (haystack.charAt(i+j) != needle.charAt(j)) {
break;
}
}
if (j == needle.length()) {
return i;
}
}
}
}
return -1;
}
}
在第一层循环中改变了终止条件,使得速度上快很多,另外少了index变量,空间复杂度也降了。在核心判断条件:haystack.charAt(i+j) != needle.charAt(j)中,使用了双指针的思想。
LeetCode & Q28-Implement strStr-Easy的更多相关文章
- 【leetcode】Implement strStr() (easy)
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 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()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 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 ...
- 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()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- Linux tar包安装Nginx-1.7.6 (yum方式安装依赖)
1.首先安装依赖包(依赖包有点多,我们采用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...
- 数组Array、数组API
1.数组:批量管理多个数据的存储空间. 数组的作用:现实中,批量管理多个数据都是集中分组存放,良好的数据结构,可极大提高程序的执行效率! 优点:方便查找 2.创建数组:(4种方式) (1)var 变量 ...
- leaflet渲染mapbox gl的矢量数据
准备条件 1.mapbox-gl.js mapbox-gl.css 2.leaflet-mapbox-gl.js https://github.com/mapbox/mapbox-gl-leaflet ...
- Unity的常用API
1.Event Function:事件函数 Reset() :被附加脚本时.在游戏物体的组件上按Reset时会触发该事件函数 Start() :在游戏初始化时会执行一次 Update() :每一帧 ...
- 关于mongodb按照字段模糊查询方法
模糊查询:tname包含某个关键字测试' cd /opt/soft/mongodb/bin ./mongo --host 192.168.0.1 --port 17017 test db.test ...
- 基于hi-nginx的web开发(python篇)——路由装饰器
现在,有了起步的基本认识,现在需要一个可以媲美flask或者bottle的简洁易用的路由功能,可以用装饰器写法任意映射 URLs 到代码. 这个,并不难.首先,来一个叫做hi的模块:hi.py: im ...
- linux 的tee命令
tee 如果你在linux下希望将程序或命令运行的信息,在输入到文件的同时,也能够显示在屏幕上,你可以考虑使用tee这个命令.举个例子,直接上图 这里我调用函数aaa来完成将结果输入到aaa.log里 ...
- http,socks4,socks5代理的区别
HTTP代理 能够代理客户机的HTTP访问,主要是代理浏览器访问网页,它的端口一般为80.8080.3128等: SOCKS代理 SOCKS代理与其他类型的代理不同,它只是简单地传递数据包,而并不关心 ...
- Nginx目录浏览功能
要给其他人提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让其他人看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件:效果如图. 实现步骤:在虚拟主机配置文件里面开 ...
- MyBatis-plus 代码生成器
1.添加pom文件依赖 <!-- Mybatis-Plus 自动生成实体类--> <dependency> <groupId>com.baomidou</gr ...