代码随想录算法训练营第八天-28实现 strStr() 、459重复的子字符串
初步题解
28 实现 strStr()
题目链接:(https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/description/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class LE28 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String haystack = scanner.nextLine(); String needle = scanner.nextLine(); System.out.println(strStr(haystack, needle)); }
public static int strStr(String haystack, String needle) { int j = 0; for (int i = 0; i < haystack.length(); i++) { if (haystack.charAt(i) == needle.charAt(j)) { if (j == needle.length() - 1) { return i - j; } j++; } else { i = i - j; j = 0; } } return -1; } }
|
459 重复的子字符串
题目链接:(https://leetcode.cn/problems/repeated-substring-pattern/description/)
(虽然讲了 KMP 应该就用 KMP,但是不会)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class LE459 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String nextLine = scanner.nextLine(); System.out.println(repeatedSubstringPattern(nextLine)); } public static boolean repeatedSubstringPattern(String s) { int n = s.length(); for (int i = 1; i * 2 <= n; i++) { if (n % i == 0) { boolean match = true; for (int j = i; j < n; j++) { if (s.charAt(j) != s.charAt(j - i)) { match = false; break; } } if (match) { return true; } } } return false; } }
|
看讲解
28 实现 strStr()
题目链接/文章讲解/视频讲解:(https://programmercarl.com/0028.实现strStr.html)
本题应该用 KMP 思路,这个算法主要用在字符串匹配上。KMP 的经典思想就是:当出现字符串不匹配时,可以记录一部分之前已经匹配的文本内容,利用这些信息避免从头再去做匹配。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
public static int strStr1(String haystack, String needle) { int[] ints = new int[needle.length()]; int[] next = getNext(ints, needle); int j = 0; if (needle.length() == 0) { return -1; } for (int i = 0; i < haystack.length(); i++) { while (j != 0 && haystack.charAt(i) != needle.charAt(j)) { j = next[j - 1]; } if (haystack.charAt(i) == needle.charAt(j)) { if (j == needle.length() - 1) { return i - j; } j++; } } return -1; }
private static int[] getNext(int[] next, String s) { int j = 0; next[0] = j; for (int i = 1; i < s.length(); i++) { while (j != 0 && s.charAt(i) != s.charAt(j)) { j = next[j - 1]; } if (s.charAt(i) == s.charAt(j)) { j++; } next[i] = j; } return next; }
|
459 重复的子字符串
题目链接/文章讲解/视频讲解:(https://programmercarl.com/0459.重复的子字符串.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
public static boolean repeatedSubstringPattern1(String s) { int[] next = getNext(s); return next[s.length() - 1] != 0 && s.length() % (s.length() - next[s.length() - 1]) == 0; } private static int[] getNext(String s) { int j = 0; int[] next = new int[s.length()]; next[j] = 0; for (int i = 1; i < s.length(); i++) { while (j != 0 && s.charAt(i) != s.charAt(j)) { j = next[j - 1]; } if (s.charAt(i) == s.charAt(j)) { j++; } next[i] = j; } return next; }
|