文章目录
一、打乱数组

class Solution {
int[] nums;
int[] tempArr;
public Solution(int[] nums) {
this.nums = nums.clone();
tempArr = nums.clone();
}
public int[] reset() {
return nums;
}
public int[] shuffle() {
int r1 = new Random().nextInt(nums.length);
int r2 = new Random().nextInt(nums.length);
int temp = tempArr[r1];
tempArr[r1] = tempArr[r2];
tempArr[r2] = temp;
return tempArr;
}
}
二、最小栈

class MinStack {
private ListNode head;
public void push(int x) {
if (empty()) {
head = new ListNode(x, x, null);
} else {
head = new ListNode(x, Math.min(x, head.min), head);
}
}
public void pop() {
if (empty()) {
throw new IllegalStateException("Stack is Empty");
}
head = head.next;
}
public int top() {
if (empty()) {
throw new IllegalStateException("Stack is Empty");
}
return head.val;
}
public int getMin() {
if (empty()) {
throw new IllegalStateException("Stack is Empty");
}
return head.min;
}
private boolean empty() {
return head == null;
}
}
class ListNode {
public int val;
public int min;
public ListNode next;
public ListNode(int val, int min, ListNode next) {
this.val = val;
this.min = min;
this.next = next;
}
}