第三节练习

pull/3/head
Leo 5 years ago
parent 11c46f9587
commit cdbdf836b8

@ -219,6 +219,60 @@ public class DeleteGivenValue {
return head;
}
public static DoubleNode removeDoubleNodeOfValue4(DoubleNode head, int value) {
while (head != null) {
if (head.value != value) {
break;
}
head = head.next;
}
if (head != null) {
head.pre = null;
}
DoubleNode pre = head;
DoubleNode cur = head;
while (cur != null) {
if (cur.value == value) {
pre.next = cur.next;
}else{
if (cur.pre != pre) {
cur.pre = pre;
}
pre = cur;
}
cur = cur.next;
}
return head;
}
public static DoubleNode removeDoubleNodeOfValue5(DoubleNode head, int value) {
while (head != null) {
if (head.value != value) {
break;
}
head = head.next;
}
if (head != null && head.pre != null) {
head.pre = null;
}
DoubleNode pre = head;
DoubleNode cur = head;
while (cur != null) {
if (cur.value == value) {
pre.next = cur.next;
}else{
if (cur.pre != pre) {
cur.pre = pre;
}
pre = cur;
}
cur = cur.next;
}
return head;
}
/**
* :
* @author Leo
@ -332,7 +386,7 @@ public class DeleteGivenValue {
break;
}
DoubleNode doubleNodeHead = randomDoubleNode(sizeMax, range);
DoubleNode doubleNode = removeDoubleNodeOfValue3(doubleNodeHead, value);
DoubleNode doubleNode = removeDoubleNodeOfValue5(doubleNodeHead, value);
if (!verifyRemoveDoubleNodeOfValue(doubleNode, value)) {
System.out.println("doubleNode fuck");
break;

@ -0,0 +1,55 @@
package leo.class02;
import leo.util.ArrayUtil;
import java.util.Arrays;
/**
* @author Leo
* @ClassName GetMax
* @DATE 2020/11/21 7:52
* @Description
*/
public class GetMax {
public static int getMax(int[] arr) {
int max = process(arr, 0, arr.length - 1);
return max;
}
private static int process(int[] arr, int L, int R) {
if (L == R) {
return arr[L];
}
int mid = L + ((R - L) >> 1);
int leftMax = process(arr, 0, mid);
int rightMax = process(arr, mid + 1, R);
return Math.max(leftMax, rightMax);
}
private static int testGetMax(int[] arr) {
Arrays.sort(arr);
return arr[arr.length - 1];
}
public static void main(String[] args){
int maxSize = 10;
int range = 100;
int testTime = 100;
System.out.println("Start!");
for (int i = 0; i < testTime; i++) {
int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range);
int max = getMax(arr);
int testMax = testGetMax(arr);
if (max != testMax) {
System.out.println("fuck");
break;
}
}
System.out.println("End!");
}
}

@ -14,7 +14,7 @@ import java.util.Stack;
public class GetMinStack {
/**
* ,
*
*/
public static class MyStack1{
@ -50,6 +50,45 @@ public class GetMinStack {
}
public static class MyStack3{
private Stack<Integer> data;
private Stack<Integer> min;
public MyStack3() {
data = new Stack<>();
min = new Stack<>();
}
public void push(int value) {
if (min.isEmpty() || this.getMin() >= value) {
min.push(value);
}
data.push(value);
}
public Integer pop() {
if (data.isEmpty()) {
throw new RuntimeException("Stack is Empty");
}
int value = data.pop();
if (value == this.getMin()) {
min.pop();
}
return value;
}
public Integer getMin() {
if (min.isEmpty()) {
throw new RuntimeException("stack is empty");
}
return min.peek();
}
}
/**
@ -94,6 +133,45 @@ public class GetMinStack {
}
public static class MyStack4{
private Stack<Integer> data;
private Stack<Integer> min;
public MyStack4() {
this.data = new Stack<>();
this.min = new Stack<>();
}
public void push(int value) {
try {
if (data.isEmpty() || this.getMin() > value) {
this.min.push(value);
} else {
min.push(this.getMin());
}
} catch (Exception e) {
this.min.push(value);
}
this.data.push(value);
}
public Integer pop() throws Exception {
if (data.isEmpty()) {
throw new Exception("Stack is Empty!");
}
this.min.pop();
return this.data.pop();
}
public int getMin() throws Exception {
if (this.min.isEmpty()) {
throw new Exception("stack is empty");
}
return min.peek();
}
}
/**
* list
*/
@ -184,7 +262,7 @@ public class GetMinStack {
System.out.println("Start!");
for (int i = 0; i < testTime; i++) {
MyStackOfList myStack = new MyStackOfList();
MyStack4 myStack = new MyStack4();
TestMyStack testMyStack = new TestMyStack();
for (int j = 0; j < forTime; j++) {
if (Math.random() < 0.5) {

@ -130,6 +130,31 @@ public class LinkedList {
return pre;
}
public static Node reverseNode7(Node head) {
Node pre = head;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public static Node reverseNode8(Node head) {
Node pre = head;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public static DoubleNode reverseDoubleNode(DoubleNode head) {
DoubleNode pre = null;
DoubleNode next;
@ -183,6 +208,20 @@ public class LinkedList {
}
public static DoubleNode reverseDoubleNode4(DoubleNode head) {
DoubleNode pre = head;
DoubleNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
head.pre = next;
pre = head;
head = next;
}
return pre;
}
/**
* :
* @author Leo
@ -313,14 +352,14 @@ public class LinkedList {
for (int i = 0; i < testTime; i++) {
Node head = randomNode(maxSize, range);
List<Node> nodeList = nodeToList(head);
Node node = reverseNode6(head);
Node node = reverseNode8(head);
if (!verifyReverseListAndNode(nodeList, node)) {
System.out.println("nodeFuck!!");
break;
}
DoubleNode doubleNodeHead = randomDoubleNode(maxSize, range);
List<DoubleNode> doubleNodeList = DoubleNodeToList(doubleNodeHead);
DoubleNode doubleNode = reverseDoublerNode3(doubleNodeHead);
DoubleNode doubleNode = reverseDoubleNode4(doubleNodeHead);
if (!verifyReverseListAndDoubleNode(doubleNodeList, doubleNode)) {
System.out.println("doubleNodeFuck!!");
break;

@ -8,12 +8,12 @@ import java.util.LinkedList;
* @author Leo
* @ClassName RingArray
* @DATE 2020/11/20 2:53
* @Description 使
* @Description 使
*/
public class RingArray {
/**
* 使
* 使
*/
public static class MyQueue {
int[] arr;
@ -57,13 +57,56 @@ public class RingArray {
}
public static class MyQueue1{
private int[] arr;
private int size;
private int push;
private int poll;
private final int limit;
public MyQueue1(int limit) {
arr = new int[limit];
this.size = 0;
this.push = 0;
this.poll = 0;
this.limit = limit;
}
public void push(int value) {
if (size == limit) {
throw new RuntimeException("队列满了");
}
arr[push] = value;
size++;
push = nextIndex(push);
}
public int poll() {
if (size == 0) {
throw new RuntimeException("队列空了");
}
int value = arr[poll];
size--;
poll = nextIndex(poll);
return value;
}
public int nextIndex(int i) {
return i < limit - 1 ? i + 1 : 0;
}
public boolean isEmpty() {
return size == 0;
}
}
public static void main(String[] args) {
int testTime = 10000;
int range = 100;
int sizeMax = 80;
for (int i = 0; i < testTime; i++) {
int length = randomInt(sizeMax);
MyQueue myQueue = new MyQueue(length);
MyQueue1 myQueue = new MyQueue1(length);
Queue<Integer> queue = new LinkedList<>();
for (int j = 0; j < length; j++) {
int value = randomInt(range);

Loading…
Cancel
Save