modify code

pull/6/head
左程云 5 years ago
parent a3bc767b31
commit 47ff0ddf6d

@ -1,23 +1,21 @@
package class27; package class27;
public class Code01_KMP { public class Code01_KMP {
// O(N)
public static int getIndexOf(String s, String m) { public static int getIndexOf(String s, String m) {
if (s == null || m == null || m.length() < 1 || s.length() < m.length()) { if (s == null || m == null || m.length() < 1 || s.length() < m.length()) {
return -1; return -1;
} }
char[] str = s.toCharArray(); char[] str = s.toCharArray();
char[] match = m.toCharArray(); char[] match = m.toCharArray();
int x = 0; // str中当前比对到的位置 int x = 0;
int y = 0; // match中当前比对到的位置 int y = 0;
// M M <= N O(M) int[] next = getNextArray(match);
int[] next = getNextArray(match); // next[i] match中i之前的字符串match[0..i-1]
// O(N)
while (x < str.length && y < match.length) { while (x < str.length && y < match.length) {
if (str[x] == match[y]) { if (str[x] == match[y]) {
x++; x++;
y++; y++;
} else if (next[y] == -1) { // y == 0 } else if (next[y] == -1) {
x++; x++;
} else { } else {
y = next[y]; y = next[y];
@ -26,7 +24,6 @@ public class Code01_KMP {
return y == match.length ? x - y : -1; return y == match.length ? x - y : -1;
} }
// M O(M)
public static int[] getNextArray(char[] match) { public static int[] getNextArray(char[] match) {
if (match.length == 1) { if (match.length == 1) {
return new int[] { -1 }; return new int[] { -1 };
@ -35,10 +32,9 @@ public class Code01_KMP {
next[0] = -1; next[0] = -1;
next[1] = 0; next[1] = 0;
int i = 2; int i = 2;
// cn代表cn位置的字符是当前和i-1位置比较的字符
int cn = 0; int cn = 0;
while (i < next.length) { while (i < next.length) {
if (match[i - 1] == match[cn]) { // 跳出来的时候 if (match[i - 1] == match[cn]) {
next[i++] = ++cn; next[i++] = ++cn;
} else if (cn > 0) { } else if (cn > 0) {
cn = next[cn]; cn = next[cn];

@ -14,23 +14,19 @@ public class Code02_TreeEqual {
} }
} }
// big做头节点的树其中是否有某棵子树的结构是和small为头的树完全一样的
public static boolean containsTree1(Node big, Node small) { public static boolean containsTree1(Node big, Node small) {
if (small == null) { if (small == null) {
return true; return true;
} }
// small != null
if (big == null) { if (big == null) {
return false; return false;
} }
// big=null small!=null
if (isSameValueStructure(big, small)) { if (isSameValueStructure(big, small)) {
return true; return true;
} }
return containsTree1(big.left, small) || containsTree1(big.right, small); return containsTree1(big.left, small) || containsTree1(big.right, small);
} }
// head1为头的树是否在结构对应上完全和head2一样
public static boolean isSameValueStructure(Node head1, Node head2) { public static boolean isSameValueStructure(Node head1, Node head2) {
if (head1 == null && head2 != null) { if (head1 == null && head2 != null) {
return false; return false;
@ -44,7 +40,6 @@ public class Code02_TreeEqual {
if (head1.value != head2.value) { if (head1.value != head2.value) {
return false; return false;
} }
// head1.value == head2.value
return isSameValueStructure(head1.left, head2.left) return isSameValueStructure(head1.left, head2.left)
&& isSameValueStructure(head1.right, head2.right); && isSameValueStructure(head1.right, head2.right);
} }

Loading…
Cancel
Save