【LeetCode】 121 实现 strStr()
题目:
这道题力扣和牛客的题目略微不同,所以我以力扣为准
解题思路:
滑动窗口
http://leetcode-cn.com/problems/implement-strstr/solution/shi-xian-strstr-by-leetcode/
代码:
public class LC122 {
public int strStr(String haystack, String needle) {
int L = needle.length(), n = haystack.length();
for (int start = 0; start < n - L + 1; ++start) {
if (haystack.substring(start, start + L).equals(needle)) {
return start;
}
}
return -1;
}
public static void main(String[] args) {
LC122 lc122 = new LC122();
System.out.println(lc122.strStr("noddle", "ddle"));
}
}
「其他文章」
- GoLand控制台显示太多
- LeetCode 470. 用 Rand7() 实现 Rand10()
- 手撕堆排序
- Redis主从同步
- 物理内存与虚拟内存
- JVM init和clinit方法区别
- HTTP报文格式
- 【LeetCode】 144 二叉树的层序遍历
- Secondary NameNode和Standby NameNde的区别
- 【LeetCode】 143 二叉树的后序遍历
- 【LeetCode】 141 接雨水
- 【LeetCode】 140 柱状图中最大的矩形
- RDD的action操作到底指的是什么
- RDD转换算子和行动算子的区别
- Scala基础 - 下划线使用指南
- 【LeetCode】 138 寻找两个有序数组的中位数
- 【LeetCode】 135 K个一组翻转链表
- 【LeetCode】 130 最长不含重复字符的子字符串
- 【LeetCode】 128 z字形变换
- 【LeetCode】 126 正则表达式匹配