ARTS是什么? Algorithm:每周至少做一个LeetCode的算法题 Review:阅读并点评至少一篇英文技术文章 Tip:学习至少一个技术技巧,总结和归纳日常工作中遇到的知识点 Share:分享一篇有观点和思考的技术文章
Algorithm
完成leetcode算法第28题。
由于时间关系只用最简单的BF算法做了一次,等后续有时间会把BM和KMP的方式在实现一次
package com.ohyoung.leetcode.match;
/**
* 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的
第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
*
* 示例 1:
*
* 输入:haystack = "hello", needle = "ll"
* 输出:2
* 示例 2:
*
* 输入:haystack = "aaaaa", needle = "bba"
* 输出:-1
* 示例 3:
*
* 输入:haystack = "", needle = ""
* 输出:0
* @author vince
*/
public class StrStr28 {
/**
* todo 暂时使用最简单的BF算法,有时间了会将BM和KMP算法用进去
*/
public static void main(String[] args) {
System.out.println(strStr("a", "a"));
System.out.println(strStr("abc", "c"));
System.out.println(strStr("hello", "ll"));
System.out.println(strStr("aaaaa", "bba"));
System.out.println(strStr("", ""));
}
public static int strStr(String haystack, String needle) {
return BF(haystack, haystack.length(), needle, needle.length());
}
private static int BF(String haystack, int n, String needle, int m) {
if (needle == null) {
return -1;
}
if ("".equals(needle)) {
return 0;
}
if (haystack == null || "".equals(haystack)) {
return -1;
}
if (haystack.equals(needle)) {
return 0;
}
for (int i = 0; i < n - m + 1; i++) {
String subStr = haystack.substring(i, i + m);
if (needle.equals(subStr)) {
return i;
}
}
return -1;
}
}
Review
About Go Language — An Overview
它讲述了Go的演进历史、特性的灵感来源、优缺点以及被使用的曲线图,是一篇不错的了解Go语言全貌的概述文章。
Tip
什么是索引的区分度?
区分度=count(distinct index) / count(*);如果区分度越小,证明该索引在数据库中的重复项越多,mysql有可能会扫全表,因为索引不能很好的区分数据并且还会有额外的回表的性能损耗。所以,我们建索引的一个前提是在索引长度合适的情况下,区分度越高越好。
Share
输出文章 - 字符串匹配算法(一)- BF、RK算法
Finish
预计完成时间:2021.08.16 ~ 2021.08.22
实际完成时间:2021.08.22