ARTS是什么? Algorithm:每周至少做一个LeetCode的算法题 Review:阅读并点评至少一篇英文技术文章 Tip:学习至少一个技术技巧,总结和归纳日常工作中遇到的知识点 Share:分享一篇有观点和思考的技术文章

Algorithm

完成leetcode算法第28题。

由于时间关系只用最简单的BF算法做了一次,等后续有时间会把BM和KMP的方式在实现一次

  1. package com.ohyoung.leetcode.match;
  2. /**
  3. * 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的
  4. 第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
  5. *
  6. * 示例 1:
  7. *
  8. * 输入:haystack = "hello", needle = "ll"
  9. * 输出:2
  10. * 示例 2:
  11. *
  12. * 输入:haystack = "aaaaa", needle = "bba"
  13. * 输出:-1
  14. * 示例 3:
  15. *
  16. * 输入:haystack = "", needle = ""
  17. * 输出:0
  18. * @author vince
  19. */
  20. public class StrStr28 {
  21. /**
  22. * todo 暂时使用最简单的BF算法,有时间了会将BM和KMP算法用进去
  23. */
  24. public static void main(String[] args) {
  25. System.out.println(strStr("a", "a"));
  26. System.out.println(strStr("abc", "c"));
  27. System.out.println(strStr("hello", "ll"));
  28. System.out.println(strStr("aaaaa", "bba"));
  29. System.out.println(strStr("", ""));
  30. }
  31. public static int strStr(String haystack, String needle) {
  32. return BF(haystack, haystack.length(), needle, needle.length());
  33. }
  34. private static int BF(String haystack, int n, String needle, int m) {
  35. if (needle == null) {
  36. return -1;
  37. }
  38. if ("".equals(needle)) {
  39. return 0;
  40. }
  41. if (haystack == null || "".equals(haystack)) {
  42. return -1;
  43. }
  44. if (haystack.equals(needle)) {
  45. return 0;
  46. }
  47. for (int i = 0; i < n - m + 1; i++) {
  48. String subStr = haystack.substring(i, i + m);
  49. if (needle.equals(subStr)) {
  50. return i;
  51. }
  52. }
  53. return -1;
  54. }
  55. }

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