赞
踩
这题目也是经典了,如果要在原地实现的话,先整体reverse,再分别reverse前k个和后面剩余的,属于没见过不好想的类型
但Java的话不能原地实现,这里就当学习Java内置字符串方法了
String和StringBuilder都有substring方法,一个参数时该参数表示开始位置(包含),两个参数时第一个参数表示开始位置,第二个参数表示结束位置,左闭右开
实际使用发现StringBuilder的拼接不能用+,因为StringBuilder类没有重载加号运算符,需要使用append来拼接
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = Integer.parseInt(in.nextLine());
- String s = in.nextLine();
- StringBuilder s1 = new StringBuilder(s.substring(0,s.length()-n));
- StringBuilder s2 = new StringBuilder(s.substring(s.length()-n));
- s2.append(s1);
- System.out.println(s2);
-
- }
- }
一眼KMP,虽然但是暴力能过.........
- class Solution {
- public int strStr(String haystack, String needle) {
- int n = haystack.length(), m = needle.length();
- for (int i = 0; i + m <= n; i++) {
- boolean flag = true;
- for (int j = 0; j < m; j++) {
- if (haystack.charAt(i+j) != needle.charAt(j)) {
- flag = false;
- break;
- }
- }
- if (flag) {
- return i;
- }
- }
- return -1;
- }
- }

下面开始用KMP来优化,这样当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配来实现优化。
首先求出前缀表next数组(这里我直接用前缀表作为next数组同时首位设为-1标记,个人感觉最简单)。前缀表是用来回退的,当前位置匹配失败,会找到之前已经匹配上的位置,再重新匹配,此也意味着在某个字符失配时,前缀表会告诉你下一步匹配中,模式串应该跳到哪个位置。
求前缀表:动态规划解决,i位置的前缀表值取决于i-1位置模式串的字符和i-1位置的前缀表值
- public static int[] getNextArray(char[] str2) {
- if (str2.length == 1) {
- return new int[] { -1 };
- }
- int[] next = new int[str2.length];
- next[0] = -1;
- next[1] = 0;
- int i = 2; // 目前在哪个位置上求next数组的值
- int cn = 0; // 当前是哪个位置的值再和i-1位置的字符比较
- while (i < next.length) {
- if (str2[i - 1] == str2[cn]) { // 配成功的时候
- next[i++] = ++cn;
- } else if (cn > 0) {
- cn = next[cn];
- } else {
- next[i++] = 0;
- }
- }
- return next;
- }

接下来使用前缀表匹配字符串。循环内三种情况:当匹配成功时,两个指针都向后移动;不成功时递归移动y=next[y],直到next[y] == -1,主串指针移到下一个位置
- public static int getIndexOf(String s1, String s2) {
- if (s1 == null || s2 == null || s2.length() < 1 || s1.length() < s2.length()) {
- return -1;
- }
- char[] str1 = s1.toCharArray();
- char[] str2 = s2.toCharArray();
- int x = 0;
- int y = 0;
- // O(M) m <= n
- int[] next = getNextArray(str2);
- // O(N)
- while (x < str1.length && y < str2.length) {
- if (str1[x] == str2[y]) {
- x++;
- y++;
- } else if (next[y] == -1) { // y == 0
- x++;
- } else {
- y = next[y];
- }
- }
- return y == str2.length ? x - y : -1;
- }

leetcode完整代码
- class Solution {
- public int strStr(String haystack, String needle) {
- if (haystack == null || needle == null || haystack.length()< needle.length()) return -1;
- char[] str1 = haystack.toCharArray();
- char[] str2 = needle.toCharArray();
- int[] next = getNext(str2);
-
- int p1 = 0, p2 = 0;
- while(p1 < str1.length && p2 < str2.length) {
- if(str1[p1] == str2[p2]) {
- p1++;
- p2++;
- } else if (next[p2] == -1){
- p1++;
- } else {
- p2 = next[p2];
- }
-
- }
- return p2 == str2.length? p1-p2:-1;
- }
-
- public int[] getNext(char[] str) {
- if (str.length == 1) {
- return new int[] {-1};
- }
- int[] next = new int[str.length];
- next[0] = -1;
- next[1] = 0;
- int i = 2, cnt = 0;
- while (i < next.length) {
- if (str[i-1] == str[cnt]) {
- next[i++] = ++cnt;
- } else if (cnt > 0) {
- cnt = next[cnt];
- } else {
- next[i++] = 0;
- }
- }
- return next;
- }
- }

发现Java中indexOf可以直接匹配子串,先直接使用库函数
将两个字符串拼接后,去除头尾,如果还能找到那个该字符串的话,说明该字符串内部可拼
- class Solution {
- public boolean repeatedSubstringPattern(String s) {
- String ss = (s+s).substring(1,s.length()*2-1);
- int index = ss.indexOf(s);
- return index == -1? false:true;
- }
- }
接下来使用KMP。如果一个字符串 s
可以由某个子串重复构成,那么 s.length() % (s.length() - next[s.length()])
的结果必定等于 0。
这是因为在 KMP 算法中,next[len]
表示的是字符串 s
的最长前缀和最长后缀相等的长度。如果 s
是由某个子串重复构成的,那么整个字符串 s
的长度一定是子串长度的倍数。
- class Solution {
- public boolean repeatedSubstringPattern(String s) {
- char[] str = s.toCharArray();
- int[] next = getNext(str);
- if (next[str.length-1] != 0 && str.length % (str.length - next[str.length-1]) == 0) {
- return true;
- }
- return false;
- }
-
- public int[] getNext(char[] str) {
- if (str.length == 1) return new int[] {-1};
- int[] next = new int[str.length];
- next[0] = -1;
- next[1] = 0;
- int i = 2, cnt = 0;
- while(i < str.length) {
- if (str[i-1] == str[cnt]) {
- next[i++] = ++cnt;
- } else if (cnt > 0) {
- cnt = next[cnt];
- } else {
- next[i++] = 0;
- }
- }
- return next;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。