目录
一.栈
1.getmin栈
class MyStack{
public MyStack(Stack<Integer> stackData, Stack<Integer> stackMin) {
this.stackData = stackData;
this.stackMin = stackMin;
}
private Stack<Integer> stackData; //存所有值的
private Stack<Integer> stackMin;//始终存存的是最小值
public void push(int num){
if(this.stackMin.empty()){
this.stackMin.push(num);
}else if (num< this.getMin()){
this.stackMin.push(num);
}
this.stackData.push(num);
}
public int pop(){
if(this.stackData.empty()){
throw new RuntimeException("empty stack");
}
int value=this.stackData.pop();
if(this.getMin()==value){
this.stackMin.pop();
}
return value;
}
public int getMin(){
if(this.stackMin.empty()){
throw new RuntimeException("empty stack");
}
return this.stackMin.peek();
}
}
2.猫狗队列
public static class Pet {
private String type;
public Pet(String type) {
this.type = type;
}
public String getPetType() {
return this.type;
}
}
public static class Dog extends Pet {
public Dog() {
super("dog");
}
}
public static class Cat extends Pet {
public Cat() {
super("cat");
}
}
public static class PetEnterQueue {
private Pet pet;
private long count;
public PetEnterQueue(Pet pet, long count) {
this.pet = pet;
this.count = count;
}
public Pet getPet() {
return this.pet;
}
public long getCount() {
return this.count;
}
public String getEnterPetType() {
return this.pet.getPetType();
}
}
public static class DogCatQueue {
//queue的方法
//尾部添加
//boolean add(E e);
//boolean offer(E e);
//他们的共同之处是建议实现类禁止添加 null 元素,否则会报空指针 NullPointerException;
//不同之处在于 add() 方法在添加失败(比如队列已满)时会报 一些运行时错误 错;而 offer() 方法即使在添加失败时也不会奔溃,只会返回 false。
//删除返回头部
//E remove();
//E poll();
//当队列为空时 remove() 方法会报 NoSuchElementException 错; 而 poll() 不会奔溃,只会返回 null。
//返回头部
//E element();
//E peek();
//当队列为空时 element() 抛出异常;peek() 不会奔溃,只会返回 null。
private Queue<PetEnterQueue> dogQ;
private Queue<PetEnterQueue> catQ;
private long count;
//1.add方法将cat类或dog类的实例放入队列中
//2.pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;
//3.pollDog方法,将队列中dog类的实例按照进队列的先后顺序依次弹出;
//4.pollCat方法,将队列中cat类的实例按照进队列的先后顺序依次弹出;
//5.isEmpty方法,检查队列中是否还有dog或cat的实例;
//6.isDogEmpty方法,检查队列中是否有dog类的实例;
//7.isCatEmpty方法,检查队列中是否有cat类的实例。
public DogCatQueue() {
this.dogQ = new LinkedList<PetEnterQueue>();
this.catQ = new LinkedList<PetEnterQueue>();
this.count = 0;
}
public void add(Pet pet) {
if (pet.getPetType().equals("dog")) {
this.dogQ.add(new PetEnterQueue(pet, this.count++));
} else if (pet.getPetType().equals("cat")) {
this.catQ.add(new PetEnterQueue(pet, this.count++));
} else {
throw new RuntimeException("err, not dog or cat");
}
}
public Pet pollAll() {
if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
//总是数量多的那个后入队列的
if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) {
return this.dogQ.poll().getPet();
} else {
return this.catQ.poll().getPet();
}
} else if (!this.dogQ.isEmpty()) {
return this.dogQ.poll().getPet();
} else if (!this.catQ.isEmpty()) {
return this.catQ.poll().getPet();
} else {
throw new RuntimeException("err, queue is empty!");
}
}
public Dog pollDog() {
if (!this.isDogQueueEmpty()) {
return (Dog) this.dogQ.poll().getPet();
} else {
throw new RuntimeException("Dog queue is empty!");
}
}
public Cat pollCat() {
if (!this.isCatQueueEmpty()) {
return (Cat) this.catQ.poll().getPet();
} else
throw new RuntimeException("Cat queue is empty!");
}
public boolean isEmpty() {
return this.dogQ.isEmpty() && this.catQ.isEmpty();
}
public boolean isDogQueueEmpty() {
return this.dogQ.isEmpty();
}
public boolean isCatQueueEmpty() {
return this.catQ.isEmpty();
}
}
3.一个栈实现另外栈的排序
public static void sortStackByStack(Stack<Integer> stack) {
Stack<Integer> help = new Stack<Integer>();
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!help.isEmpty() && help.peek() < cur) {
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty()) {
stack.push(help.pop());
}
}
二.链表
1.print 2个有序链表的公共部分
class Node{
public int value;
public Node next;
public Node(int data){
this.value=data;
}
}
public class NodeTest {
public void printCommonPart(Node head1,Node head2){
while(head1!=null&&head2!=null){
if (head1.value<head2.value){
head1=head1.next;
}else if (head1.value>head2.value){
head2=head2.next;
}else{
System.out.println(head1.value);
head1=head1.next;
head2=head2.next;
}
}
}
}
2.单双链表删除倒数第k个节点
//单
//类似差值
public static Node removeLastKthNode(Node head, int lastKth) {
if (head == null || lastKth < 1) {
return head;
}
Node cur = head;
while (cur != null) {
lastKth--;
cur = cur.next;
}
if (lastKth == 0) {
head = head.next;
}
if (lastKth < 0) {
cur = head;
while (++lastKth != 0) {
cur = cur.next;
}
cur.next = cur.next.next;
}
return cur;
}
//栈
//双指针
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
// 让快指针先走k步,和慢指针相差k个距离
for (int i = k; i > 0; i--) {
fast = fast.next;
}
// 此时让慢指针和快指针同时走,知道快指针到达链表末尾为null时,慢指针就在倒数第k个位置上了
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
// 直接返回慢指针即为答案
return slow;
}
}
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
// 让快指针先走k步,和慢指针相差k个距离
for (int i = k; i > 0; i--) {
fast = fast.next;
}
// 此时让慢指针和快指针同时走,知道快指针到达链表末尾为null时,慢指针就在倒数第k个位置上了
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
// 直接返回慢指针即为答案
return slow;
}
}
//作者:linzeliang1222
//链接:https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/solution/jian-zhi-offer-22-lian-biao-zhong-dao-sh-ixsa/
//来源:力扣(LeetCode)
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
3.删除链表的中间节点和a/b处的节点
//实现删除a/b处的节点首先要实现删除中间的节点
//找待删除节点的前一个节点
//这里面的关键逻辑是:也就是链表长度每增加2(3,5,7…),
// 要删除的节点就后移一个节点
public static Node removeMiNode(Node head){
if(head==null||head.next==null){
return head;
}
if (head.next.next==null){
return head.next;
}
Node pre=head;
Node cur=head.next.next;
while(cur.next!=null&&cur.next.next!=null){
pre=pre.next;
cur=cur.next.next;
}
pre.next=pre.next.next;
return head;
}
//如果r在区间(0,1/5]上,删除节点1;
// 如果r在区间(1/5,2/5]上,删除节点2;
// 如果r在区间(2/5,3/5]上,删除节点3;
// 如果r在区间(3/5,4/5]上,删除节点4;
// 如果r在区间(4/5,1]上,删除节点5;
// 这题的公式是该删除的节点是r=(a*n)/b向上取整
public Node removeByRatio(Node head,int a, int b ){
if(a<1||a>b) {
return head;
}
int n=0;
Node cur=head;
//这段是计算链表长度的
//是cur!=null 不是cur.next!=null
while(cur!=null){
n++;
cur= cur.next;
}
//向上取整用Math.ceil(double a)
//向下取整用Math.floor(double a)
n=(int)Math.ceil((a*n)/b);
if(n==1){
head=head.next;
}
if(n>1){
cur=head;
while(--n!=1){
cur=cur.next;
}
cur.next=cur.next.next;
}
return head;
}
4.反转单向和双向链表
//单链表的反转
//核心思想是把head.next赋值给next
//head.next置成pre
//pre置成head
//循环到head不空
public static Node reverseList(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
//旋转双链表
//双链表的结构
public static class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int data) {
this.value = data;
}
}
5.反转部分单向链表
//反转部分单向链表
//1->2->3->4->5->null,from=2,to=4
//调整结果为:1->4->3->2->5->null
//注:这里的链表长度从1开始
//找到第from-1个节点fPre和第to+1个节点tPos
//fPre就是要反转部分的前一个节点,tPos是反转部分的后一个节点。
//TODO:仔细看一下
public static Node reversePart(Node head, int from, int to) {
int len = 0;
Node node1 = head;
Node fPre = null;
Node tPos = null;
while (node1 != null) {
len++;
fPre = len == from - 1 ? node1 : fPre;
tPos = len == to + 1 ? node1 : tPos;
node1 = node1.next;
}
if (from > to || from < 1 || to > len) {
return head;
}
node1 = fPre == null ? head : fPre.next;
Node node2 = node1.next;
node1.next = tPos;
Node next = null;
while (node2 != tPos) {
next = node2.next;
node2.next = node1;
node1 = node2;
node2 = next;
}
if (fPre != null) {
fPre.next = node1;
return head;
}
return node1;
}
6.环形单链表约瑟夫
//环形单链表约瑟夫
//41个人排成一个圆圈,由第1个人开始报数,报数到3的人就自杀,
//然后再由下一个人重新报 1,报数到 3 的人再自杀,
//这样依次下去,直到剩下最后一个人时,那个人可以自由选择自己的命运。
//todo:
public static Node josephusKill1(Node head, int m) {
if (head == null || head.next == head || m < 1) {
return head;
}
Node last = head;
while (last.next != head) {
last = last.next;
}
int count = 0;
while (head != last) {
if (++count == m) {
last.next = head.next;
count = 0;
} else {
last = last.next;
}
head = last.next;
}
return head;
}
7.判断一个表是否是回文结构
搞个栈
// need n extra space
public static boolean isPalindrome1(Node head) {
Stack<Node> stack = new Stack<Node>();
Node cur = head;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (head != null) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
}
8.两个单链表组成相加链表
用栈