当前位置:   article > 正文

代码随想录day9 Java版

代码随想录day9 Java版

右旋字符串

这题目也是经典了,如果要在原地实现的话,先整体reverse,再分别reverse前k个和后面剩余的,属于没见过不好想的类型

但Java的话不能原地实现,这里就当学习Java内置字符串方法

String和StringBuilder都有substring方法,一个参数时该参数表示开始位置(包含),两个参数时第一个参数表示开始位置,第二个参数表示结束位置,左闭右开

实际使用发现StringBuilder的拼接不能用+,因为StringBuilder类没有重载加号运算符,需要使用append来拼接

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. int n = Integer.parseInt(in.nextLine());
  6. String s = in.nextLine();
  7. StringBuilder s1 = new StringBuilder(s.substring(0,s.length()-n));
  8. StringBuilder s2 = new StringBuilder(s.substring(s.length()-n));
  9. s2.append(s1);
  10. System.out.println(s2);
  11. }
  12. }

28. 实现 strStr()

一眼KMP,虽然但是暴力能过.........

  1. class Solution {
  2. public int strStr(String haystack, String needle) {
  3. int n = haystack.length(), m = needle.length();
  4. for (int i = 0; i + m <= n; i++) {
  5. boolean flag = true;
  6. for (int j = 0; j < m; j++) {
  7. if (haystack.charAt(i+j) != needle.charAt(j)) {
  8. flag = false;
  9. break;
  10. }
  11. }
  12. if (flag) {
  13. return i;
  14. }
  15. }
  16. return -1;
  17. }
  18. }

下面开始用KMP来优化,这样当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配来实现优化。

首先求出前缀表next数组(这里我直接用前缀表作为next数组同时首位设为-1标记,个人感觉最简单)。前缀表是用来回退的,当前位置匹配失败,会找到之前已经匹配上的位置,再重新匹配,此也意味着在某个字符失配时,前缀表会告诉你下一步匹配中,模式串应该跳到哪个位置。

求前缀表:动态规划解决,i位置的前缀表值取决于i-1位置模式串的字符和i-1位置的前缀表值

  1. public static int[] getNextArray(char[] str2) {
  2. if (str2.length == 1) {
  3. return new int[] { -1 };
  4. }
  5. int[] next = new int[str2.length];
  6. next[0] = -1;
  7. next[1] = 0;
  8. int i = 2; // 目前在哪个位置上求next数组的值
  9. int cn = 0; // 当前是哪个位置的值再和i-1位置的字符比较
  10. while (i < next.length) {
  11. if (str2[i - 1] == str2[cn]) { // 配成功的时候
  12. next[i++] = ++cn;
  13. } else if (cn > 0) {
  14. cn = next[cn];
  15. } else {
  16. next[i++] = 0;
  17. }
  18. }
  19. return next;
  20. }

接下来使用前缀表匹配字符串。循环内三种情况:当匹配成功时,两个指针都向后移动;不成功时递归移动y=next[y],直到next[y] == -1,主串指针移到下一个位置

  1. public static int getIndexOf(String s1, String s2) {
  2. if (s1 == null || s2 == null || s2.length() < 1 || s1.length() < s2.length()) {
  3. return -1;
  4. }
  5. char[] str1 = s1.toCharArray();
  6. char[] str2 = s2.toCharArray();
  7. int x = 0;
  8. int y = 0;
  9. // O(M) m <= n
  10. int[] next = getNextArray(str2);
  11. // O(N)
  12. while (x < str1.length && y < str2.length) {
  13. if (str1[x] == str2[y]) {
  14. x++;
  15. y++;
  16. } else if (next[y] == -1) { // y == 0
  17. x++;
  18. } else {
  19. y = next[y];
  20. }
  21. }
  22. return y == str2.length ? x - y : -1;
  23. }

leetcode完整代码

  1. class Solution {
  2. public int strStr(String haystack, String needle) {
  3. if (haystack == null || needle == null || haystack.length()< needle.length()) return -1;
  4. char[] str1 = haystack.toCharArray();
  5. char[] str2 = needle.toCharArray();
  6. int[] next = getNext(str2);
  7. int p1 = 0, p2 = 0;
  8. while(p1 < str1.length && p2 < str2.length) {
  9. if(str1[p1] == str2[p2]) {
  10. p1++;
  11. p2++;
  12. } else if (next[p2] == -1){
  13. p1++;
  14. } else {
  15. p2 = next[p2];
  16. }
  17. }
  18. return p2 == str2.length? p1-p2:-1;
  19. }
  20. public int[] getNext(char[] str) {
  21. if (str.length == 1) {
  22. return new int[] {-1};
  23. }
  24. int[] next = new int[str.length];
  25. next[0] = -1;
  26. next[1] = 0;
  27. int i = 2, cnt = 0;
  28. while (i < next.length) {
  29. if (str[i-1] == str[cnt]) {
  30. next[i++] = ++cnt;
  31. } else if (cnt > 0) {
  32. cnt = next[cnt];
  33. } else {
  34. next[i++] = 0;
  35. }
  36. }
  37. return next;
  38. }
  39. }

459.重复的子字符串

发现Java中indexOf可以直接匹配子串,先直接使用库函数

将两个字符串拼接后,去除头尾,如果还能找到那个该字符串的话,说明该字符串内部可拼

  1. class Solution {
  2. public boolean repeatedSubstringPattern(String s) {
  3. String ss = (s+s).substring(1,s.length()*2-1);
  4. int index = ss.indexOf(s);
  5. return index == -1? false:true;
  6. }
  7. }

接下来使用KMP。如果一个字符串 s 可以由某个子串重复构成,那么 s.length() % (s.length() - next[s.length()]) 的结果必定等于 0。

这是因为在 KMP 算法中,next[len] 表示的是字符串 s 的最长前缀和最长后缀相等的长度。如果 s 是由某个子串重复构成的,那么整个字符串 s 的长度一定是子串长度的倍数。

  1. class Solution {
  2. public boolean repeatedSubstringPattern(String s) {
  3. char[] str = s.toCharArray();
  4. int[] next = getNext(str);
  5. if (next[str.length-1] != 0 && str.length % (str.length - next[str.length-1]) == 0) {
  6. return true;
  7. }
  8. return false;
  9. }
  10. public int[] getNext(char[] str) {
  11. if (str.length == 1) return new int[] {-1};
  12. int[] next = new int[str.length];
  13. next[0] = -1;
  14. next[1] = 0;
  15. int i = 2, cnt = 0;
  16. while(i < str.length) {
  17. if (str[i-1] == str[cnt]) {
  18. next[i++] = ++cnt;
  19. } else if (cnt > 0) {
  20. cnt = next[cnt];
  21. } else {
  22. next[i++] = 0;
  23. }
  24. }
  25. return next;
  26. }
  27. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/752975
推荐阅读
相关标签
  

闽ICP备14008679号