parent
61dc1bbb7b
commit
12d9c32d7a
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -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]));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
public class Code04_链表实现队列 {
|
||||||
|
|
||||||
|
private static class Node<V> {
|
||||||
|
V value;
|
||||||
|
Node<V> next;
|
||||||
|
|
||||||
|
public Node(V v) {
|
||||||
|
next = null;
|
||||||
|
this.value = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MyQueue<V> {
|
||||||
|
private Node<V> head;
|
||||||
|
private Node<V> tail;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public MyQueue() {
|
||||||
|
head = null;
|
||||||
|
tail = null;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmaty() {
|
||||||
|
return size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void offer(V value) {
|
||||||
|
Node<V> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
public class Code05_链表实现栈 {
|
||||||
|
|
||||||
|
private static class Node<V> {
|
||||||
|
V value;
|
||||||
|
Node<V> next;
|
||||||
|
|
||||||
|
public Node(V v) {
|
||||||
|
next = null;
|
||||||
|
this.value = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MyStack<V> {
|
||||||
|
private Node<V> head;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public MyStack() {
|
||||||
|
head = null;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(V value) {
|
||||||
|
Node<V> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
public class Code06_链表相加 {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
public class Demo {
|
||||||
|
}
|
Loading…
Reference in new issue