1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc28.java 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-03-14 14:54 . 0314
package code;
/*
* 28. Implement strStr()
* 题意:找出子串在给定字符串的起始位置
* 难度:Easy
* 分类:Two Pointers, String
* Tips:注意判断子串为空的方法为needle.length()==0,不要用needle==""
* 最优的解法应该是O(N)的,类似KMP的思路,不过面试不会让写KMP的
*/
public class lc28 {
public static void main(String[] args) {
String haystack = "aaaaa";
String needle = "ab";
System.out.println(strStr(haystack, needle));
}
public static int strStr(String haystack, String needle) {
if(needle.length()==0)
return 0;
int cur1 = 0, cur2 = 0;
while(cur1<haystack.length()){
if(haystack.charAt(cur1)==needle.charAt(cur2)){
cur1++;
cur2++;
if(cur2==needle.length())
return cur1-cur2;
}else{
cur1++;
cur1 = cur1-cur2;
cur2 = 0;
}
}
return -1;
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891