modify code

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

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

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

Loading…
Cancel
Save