From 12d9c32d7af784150271dac18ee6b429afe723a8 Mon Sep 17 00:00:00 2001 From: xiaotiancai893661742 <46739364+xiaotiancai893661742@users.noreply.github.com> Date: Fri, 8 Jul 2022 01:35:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=A7=BB=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zuolaos/jichuban/Code00_对数器.java | 13 +++ .../jichuban/Code01_等概率事件.java | 60 ++++++++++++++ .../zuolaos/jichuban/Code02_二分法.java | 79 +++++++++++++++++++ .../zuolaos/jichuban/Code03_链表反转.java | 69 ++++++++++++++++ .../jichuban/Code04_链表实现队列.java | 67 ++++++++++++++++ .../jichuban/Code05_链表实现栈.java | 55 +++++++++++++ .../zuolaos/jichuban/Code06_链表相加.java | 6 ++ src/test/java/zuolaos/jichuban/Demo.java | 4 + 8 files changed, 353 insertions(+) create mode 100644 src/test/java/zuolaos/jichuban/Code00_对数器.java create mode 100644 src/test/java/zuolaos/jichuban/Code01_等概率事件.java create mode 100644 src/test/java/zuolaos/jichuban/Code02_二分法.java create mode 100644 src/test/java/zuolaos/jichuban/Code03_链表反转.java create mode 100644 src/test/java/zuolaos/jichuban/Code04_链表实现队列.java create mode 100644 src/test/java/zuolaos/jichuban/Code05_链表实现栈.java create mode 100644 src/test/java/zuolaos/jichuban/Code06_链表相加.java create mode 100644 src/test/java/zuolaos/jichuban/Demo.java diff --git a/src/test/java/zuolaos/jichuban/Code00_对数器.java b/src/test/java/zuolaos/jichuban/Code00_对数器.java new file mode 100644 index 0000000..1fc8517 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code00_对数器.java @@ -0,0 +1,13 @@ +package zuolaos.jichuban; + +public class Code00_对数器 { + + public static int[] generateRandomArray(int maxSize,int maxValue){ + int[] ints = new int[maxSize]; + for (int i = 0; i < ints.length; i++) { + ints[i]=(int)(Math.random()*maxValue); + } + return ints; + } + +} diff --git a/src/test/java/zuolaos/jichuban/Code01_等概率事件.java b/src/test/java/zuolaos/jichuban/Code01_等概率事件.java new file mode 100644 index 0000000..c3b92f3 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code01_等概率事件.java @@ -0,0 +1,60 @@ +package zuolaos.jichuban; + + +public class Code01_等概率事件 { + + + public static int f() { + return (int) (Math.random() * 5 + 1); + } + + public static int f1() { + int result = 0; + do { + result = f(); + } while (result == 3); + if (result == 1 || result == 2) { + return 0; + } else { + return 1; + } + } + + + public static int f2() { + int result = 0; + do { + result = (f1() << 2) + (f1() << 1) + (f1() << 0); + } while (result == 0); + return result; + } + + + public static int m() { + return Math.random() < 0.886 ? 0 : 1; + } + + public static int m1() { + int result = 0; + do { + result = (m() << 1) + (m() << 0); + } while (result == 0 || result == 3); + return result - 1; + } + + + public static void main(String[] args) { + int times = 10000000; + int[] ints = new int[10]; + + for (int i = 0; i < times; i++) { + int f = m1(); + ints[f]++; + } + + for (int i = 0; i < ints.length; i++) { + System.out.println("数字" + i + "出现的次数是" + ints[i]); + } + + } +} diff --git a/src/test/java/zuolaos/jichuban/Code02_二分法.java b/src/test/java/zuolaos/jichuban/Code02_二分法.java new file mode 100644 index 0000000..f687c91 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code02_二分法.java @@ -0,0 +1,79 @@ +package zuolaos.jichuban; + +import java.util.Arrays; + +public class Code02_二分法 { + + + public static int m1(int[] arr, int num) { + if (arr == null || arr.length == 0) { + return -1; + } + int L = 0; + int R = arr.length - 1; + int ans = -1; + while (L <= R) { + int mid = (L + R) / 2; + if (arr[mid] == num) { + ans = mid; + break; + } else if (arr[mid] < num) { + L = mid + 1; + } else { + R = mid - 1; + } + } + return ans; + } + + + // >=num 最左的数 + public static int m2(int[] arr, int num) { + if (arr == null || arr.length == 0) { + return -1; + } + int L = 0; + int R = arr.length - 1; + int ans = -1; + while (L <= R) { + int mid = (L + R) / 2; + if (arr[mid] >= num) { + ans = mid; + R = mid - 1; + } else { + L = mid + 1; + } + } + return ans; + } + + + // <=num 最右的数 + public static int m3(int[] arr, int num) { + if (arr == null || arr.length == 0) { + return -1; + } + int L = 0; + int R = arr.length - 1; + int ans = -1; + while (L <= R) { + int mid = (L + R) / 2; + if (arr[mid] <= num) { + ans = mid; + L = mid + 1; + } else { + R = mid - 1; + } + } + return ans; + } + + public static void main(String[] args) { + int[] ints = Code00_对数器.generateRandomArray(10, 60); + Arrays.sort(ints); + ints[7] = ints[8]; + System.out.println(m3(ints, ints[8])); + } + + +} diff --git a/src/test/java/zuolaos/jichuban/Code03_链表反转.java b/src/test/java/zuolaos/jichuban/Code03_链表反转.java new file mode 100644 index 0000000..dc8c3bd --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code03_链表反转.java @@ -0,0 +1,69 @@ +package zuolaos.jichuban; + +public class Code03_链表反转 { + + + private static class Node { + int value; + Node next; + + public Node(int value) { + this.value = value; + } + } + + private static class DoubleNode { + int value; + DoubleNode next; + DoubleNode last; + public DoubleNode(int value) { + this.value = value; + } + } + + public static void main(String[] args) { + + Node node = new Node(1); + node.next = new Node(2); + node.next.next = new Node(3); + Node newNode = reverNode(node); + System.out.println(newNode.value); + + + DoubleNode doubleNode = new DoubleNode(1); + DoubleNode doubleNode2 = new DoubleNode(2); + DoubleNode doubleNode3 = new DoubleNode(3); + doubleNode.next = doubleNode2; + doubleNode.next.next = doubleNode3; + doubleNode3.last=doubleNode2; + doubleNode3.last.last=doubleNode; + DoubleNode newNode2 = reverDoubleNode(doubleNode); + System.out.println(newNode2.value); + + } + + private static DoubleNode reverDoubleNode(DoubleNode head) { + DoubleNode pre=null; + DoubleNode next=null; + while (head!=null){ + next=head.next; + head.next=pre; + head.last=next; + pre=head; + head=next; + } + return pre; + } + + private static Node reverNode(Node head) { + Node pre=null; + Node next=null; + while (head!=null){ + next=head.next; + head.next=pre; + pre=head; + head=next; + } + return pre; + } +} diff --git a/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java b/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java new file mode 100644 index 0000000..8b8123c --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code04_链表实现队列.java @@ -0,0 +1,67 @@ +package zuolaos.jichuban; + +public class Code04_链表实现队列 { + + private static class Node { + V value; + Node next; + + public Node(V v) { + next = null; + this.value = v; + } + } + + private static class MyQueue { + private Node head; + private Node tail; + private int size; + + public MyQueue() { + head = null; + tail = null; + size = 0; + } + + public boolean isEmaty() { + return size == 0; + } + + public void offer(V value) { + Node cur = new Node(value); + if (tail == null) { + tail = cur; + head = cur; + } else { + tail.next = cur; + tail = cur; + } + size++; + } + + public V poll() { + V ans = null; + if (head != null) { + ans = head.value; + head = head.next; + size--; + } + if (head == null) { + tail = null; + } + return ans; + } + + public V peek() { + V ans = null; + if (head != null) { + ans = head.value; + } + return ans; + } + + + + } + +} diff --git a/src/test/java/zuolaos/jichuban/Code05_链表实现栈.java b/src/test/java/zuolaos/jichuban/Code05_链表实现栈.java new file mode 100644 index 0000000..0932dee --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code05_链表实现栈.java @@ -0,0 +1,55 @@ +package zuolaos.jichuban; + +public class Code05_链表实现栈 { + + private static class Node { + V value; + Node next; + + public Node(V v) { + next = null; + this.value = v; + } + } + + private static class MyStack { + private Node head; + private int size; + + public MyStack() { + head = null; + size = 0; + } + + public boolean isEmpty() { + return size == 0; + } + + public void push(V value) { + Node cur = new Node(value); + if (head != null) { + cur.next = head; + head = cur; + } else { + head = cur; + } + size++; + } + + public V pop() { + V ans = null; + if (head != null) { + ans = head.value; + head = head.next; + size--; + } + return ans; + } + + public V peek() { + return head == null ? null : head.value; + } + + } + +} diff --git a/src/test/java/zuolaos/jichuban/Code06_链表相加.java b/src/test/java/zuolaos/jichuban/Code06_链表相加.java new file mode 100644 index 0000000..cd7b23c --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Code06_链表相加.java @@ -0,0 +1,6 @@ +package zuolaos.jichuban; + +public class Code06_链表相加 { + + +} diff --git a/src/test/java/zuolaos/jichuban/Demo.java b/src/test/java/zuolaos/jichuban/Demo.java new file mode 100644 index 0000000..0b54162 --- /dev/null +++ b/src/test/java/zuolaos/jichuban/Demo.java @@ -0,0 +1,4 @@ +package zuolaos.jichuban; + +public class Demo { +}