pull/3/head
Leo 6 years ago
parent fb025cd392
commit 0bd6e204a7

@ -190,6 +190,40 @@ public class KM {
}
public static int onlyKTime7(int[] arr, int k, int m) {
int[] t = new int[32];
for (int num : arr) {
for (int i = 0; i < t.length; i++) {
t[i] += (num >> i) & 1;
}
}
int res = 0;
for (int i = 0; i < t.length; i++) {
if (t[i]%m!=0){
if (t[i] % m == k) {
res |= (1 << i);
} else{
return -1;
}
}
}
if (res == 0) {
int count = 0;
for (int num : arr) {
if (num == 0) {
count++;
}
}
if (count == k) {
return res;
}else{
return -1;
}
}
return res;
}
public static void main(String[] args) {
int maxKinds = 20;
@ -207,7 +241,7 @@ public class KM {
}
int[] arr = randomArray(maxKinds, range, k, m);
int ans = onlyKTime6(arr, k, m);
int ans = onlyKTime7(arr, k, m);
int ans2 = testForOnlyKTimes(arr, k, m);
if (ans != ans2) {
System.out.println(ans);

@ -112,6 +112,37 @@ public class CopyListWithRandom {
}
public static Node copyRandomNode2(Node head) {
if (head == null) {
return null;
}
Node cur = head;
Node next;
while (cur != null) {
next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
while (cur != null) {
next = cur.next.next;
cur.next.rand = cur.rand == null ? null : cur.rand.next;
cur = next;
}
cur = head;
Node copyHead = head.next;
Node copy;
while (cur != null) {
next = cur.next.next;
copy = cur.next;
cur.next = next;
copy.next = next == null ? null : next.next;
cur = next;
}
return copyHead;
}
public static void main(String[] args){
int testTime = 1000;
@ -122,7 +153,7 @@ public class CopyListWithRandom {
for (int i = 0; i < testTime; i++) {
Node node = randomNode(maxSize, range);
Node headMap = copyRandomNodeByMap(node);
Node head = copyRandomNode1(node);
Node head = copyRandomNode2(node);
Node curNode = node;
Node curHeadMap = headMap;
Node curHead = head;

@ -406,6 +406,48 @@ public class IsPalindromeList {
return verify;
}
public static boolean isPalindromeList8(Node head) {
if (head == null || head.next == null) {
return true;
}
Node n1 = head;
Node n2 = head;
while (n2.next != null && n2.next.next != null) {
n1 = n1.next;
n2 = n2.next.next;
}
//n1 is mid
n2 = n1.next;
n1.next = null;
Node n3 = null;
while (n2 != null) {
n3 = n2.next;
n2.next = n1;
n1 = n2;
n2 = n3;
}
n3 = n1;
n2 = head;
boolean verify = true;
while (n1 != null && n2 != null) {
if (n1.value != n2.value) {
verify = false;
break;
}
n1 = n1.next;
n2 = n2.next;
}
n2 = n3.next;
n3.next = null;
while (n2 != null) {
n1 = n2.next;
n2.next = n3;
n3 = n2;
n2 = n1;
}
return verify;
}
}
@ -424,7 +466,7 @@ class IsPalindromeListMain {
Node originNode = generateRandomNode(maxSize, range);
Node head = copyNode(originNode);
b = IsPalindromeList.isPalindromeListByStack(head);
a = IsPalindromeList.isPalindromeList7(head);
a = IsPalindromeList.isPalindromeList8(head);
if (!isEqualsNode(head, originNode)) {
System.out.println("not equals node!");
}

@ -1,8 +1,13 @@
package leo.class06_09;
package leo.class06_10;
import com.sun.org.apache.regexp.internal.REUtil;
import com.sun.xml.internal.bind.v2.model.core.ID;
class Node{
int value;
Node next;
public Node(int v){
this.value = v;
}
}
/**
* @author Leo
* @ClassName FindFirstIntersectNode
@ -243,6 +248,119 @@ class FindFirstIntersectNode1{
}
}
class FindFirstIntersectNode2{
public static Node getIntersectNode(Node head1,Node head2){
if (head1 == null || head1.next == null || head1.next.next == null) {
return null;
}
if (head2 == null || head2.next == null || head2.next.next == null) {
return null;
}
Node loop1 = getLoop(head1);
Node loop2 = getLoop(head2);
if (loop1 != null && loop2 != null) {
return bothLoop(head1, loop1, head2, loop2);
} else if (loop1 == null && loop2 == null) {
return noLoop(head1, head2);
}
return null;
}
public static Node getLoop(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node slow = head.next;
Node fast = head.next.next;
while (slow != fast) {
if (fast.next == null || fast.next.next == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
fast = head;
while(fast!=slow){
slow = slow.next;
fast = fast.next;
}
return slow;
}
private static Node noLoop(Node head1, Node head2) {
int n = 0;
Node cur1 = head1;
Node cur2 = head2;
while (cur1 != null) {
cur1 = cur1.next;
n++;
}
while (cur2 != null) {
cur2 = cur2.next;
n--;
}
if (cur1 != cur2) {
return null;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
if (loop1 != loop2) {
Node cur = loop1.next;
while (cur != loop1) {
if (cur == loop2) {
return loop1;
}
cur = cur.next;
}
return null;
}else{
Node cur1 = head1;
Node cur2 = head2;
int n = 0;
while (cur1 != loop1) {
n++;
cur1 = cur1.next;
}
while (cur2 != loop2) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
cur1 = cur1.next;
n--;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
}
}
class FindFirstIntersectNode_Main{
public static void main(String[] args) {
// 1->2->3->4->5->6->7->null
@ -259,7 +377,7 @@ class FindFirstIntersectNode_Main{
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value);
System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value);
// 1->2->3->4->5->6->7->4...
head1 = new Node(1);
@ -276,14 +394,17 @@ class FindFirstIntersectNode_Main{
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next; // 8->2
System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value);
System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value);
// 0->9->8->6->4->5->6..
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value);
System.out.println(FindFirstIntersectNode2.getIntersectNode(head1, head2).value);
}
}

@ -1,4 +1,4 @@
package leo.class07;
package leo.class07_10;
/**
* @author Leo

@ -1,4 +1,4 @@
package leo.class07;
package leo.class07_10;
import java.util.Stack;
@ -15,10 +15,6 @@ import java.util.Stack;
*/
public class TraversalBT {
public static void main(String[] args){
TreeNode head = new TreeNode(1);
head.left = new TreeNode(2);
@ -28,18 +24,19 @@ public class TraversalBT {
head.right.left = new TreeNode(6);
head.right.right = new TreeNode(7);
Recursive.pre(head);
Recursive.pre1(head);
System.out.println();
UnRecursive.pre(head);
UnRecursive.pre1(head);
System.out.println("--------");
Recursive.in(head);
Recursive.in1(head);
System.out.println();
UnRecursive.in(head);
UnRecursive.in1(head);
System.out.println("---------");
Recursive.pos(head);
System.out.println();
UnRecursive.pos(head);
Recursive.pos1(head);
System.out.println();
UnRecursive.pos1(head);
System.out.println("---------");
}
@ -62,6 +59,15 @@ class Recursive{
pre(node.right);
}
public static void pre1(TreeNode node) {
if (node == null) {
return;
}
System.out.print(node.value+" ");
pre1(node.left);
pre1(node.right);
}
public static void in(TreeNode node) {
if (node == null) {
return;
@ -70,6 +76,14 @@ class Recursive{
System.out.print(node.value+" ");
in(node.right);
}
public static void in1(TreeNode node){
if (node == null) {
return;
}
in1(node.left);
System.out.print(node.value+" ");
in1(node.right);
}
public static void pos(TreeNode node) {
if (node == null) {
@ -80,6 +94,14 @@ class Recursive{
System.out.print(node.value+" ");
}
public static void pos1(TreeNode node) {
if (node == null) {
return;
}
pos1(node.left);
pos1(node.right);
System.out.print(node.value + " ");
}
}
@ -93,7 +115,7 @@ class UnRecursive{
if (head == null) {
return;
}
System.out.print("pre-order: ");
System.out.println("pre-order: ");
Stack<TreeNode> stack = new Stack<>();
stack.add(head);
while (!stack.isEmpty()) {
@ -109,6 +131,26 @@ class UnRecursive{
System.out.println();
}
public static void pre1(TreeNode head){
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(head);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
System.out.print(cur.value+" ");
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
System.out.println();
}
/**
* :
*/
@ -116,7 +158,7 @@ class UnRecursive{
if (head == null) {
return;
}
System.out.print("in-order: ");
System.out.println("in-order: ");
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = head;
while (cur != null || !stack.isEmpty()) {
@ -133,7 +175,45 @@ class UnRecursive{
}
public static void in1(TreeNode head){
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = head;
while (!stack.isEmpty() || cur != null) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
System.out.print(cur.value + " ");
cur = cur.right;
}
}
System.out.println();
}
public static void in2(TreeNode head) {
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = head;
while (!stack.isEmpty() || cur != null) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
System.out.print(cur.value + " ");
cur = cur.right;
}
}
System.out.println();
}
/**
*
@ -142,7 +222,7 @@ class UnRecursive{
if (head == null) {
return;
}
System.out.print("pos-order: ");
System.out.println("pos-order: ");
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
TreeNode cur = head;
@ -164,6 +244,29 @@ class UnRecursive{
}
public static void pos1(TreeNode head){
if (head == null) {
return;
}
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
TreeNode cur = head;
s1.push(cur);
while (!s1.isEmpty()) {
cur = s1.pop();
s2.push(cur);
if (cur.left != null) {
s1.push(cur.left);
}
if (cur.right != null) {
s1.push(cur.right);
}
}
while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}
System.out.println();
}
}

@ -1,4 +1,4 @@
package leo.class07;
package leo.class07_10;
/**
* @author Leo
Loading…
Cancel
Save