From 5d30d38821022702a3d722505a0aa8d30e390d1b Mon Sep 17 00:00:00 2001 From: fanxb Date: Sun, 26 Sep 2021 13:48:56 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q146.java | 113 ++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q326.java | 12 +++ 2 files changed, 125 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q146.java create mode 100644 5.leetcode/src/com/fanxb/common/Q326.java diff --git a/5.leetcode/src/com/fanxb/common/Q146.java b/5.leetcode/src/com/fanxb/common/Q146.java new file mode 100644 index 0000000..93c1c24 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q146.java @@ -0,0 +1,113 @@ +package com.fanxb.common; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author fanxb + * @date 2021-09-17-下午11:13 + */ +public class Q146 { + private Map map; + + private int count; + private int size; + private Node head; + private Node end; + + private static class Node { + private int key; + private int value; + private Node next; + private Node last; + } + + public Q146(int capacity) { + map = new HashMap<>(capacity); + count = 0; + size = capacity; + } + + public int get(int key) { + if (count == 0) { + return -1; + } + Node node = map.get(key); + if (node == null) { + return -1; + } + moveEndToHead(node); + return node.value; + } + + private void moveEndToHead(Node node) { + if (node.last != null) { + //说明非队首元素 + node.last.next = node.next; + if (node.next != null) { + //说明非队尾元素 + node.next.last = node.last; + } else { + //说明当前元素为队尾元素,需要修改end + end = node.last; + } + //将node转移到队列头 + node.last = null; + node.next = head; + head.last = node; + head = node; + } + } + + public void put(int key, int value) { + Node node = map.get(key); + if (node != null) { + node.value = value; + //同时把node移动到首位 + moveEndToHead(node); + } else { + if (count == size) { + //说明需要排除队尾的元素 + map.remove(end.key); + if (size == 1) { + head = null; + end = null; + } else { + end = end.last; + end.next = null; + } + count--; + } + node = new Node(); + node.key = key; + node.value = value; + if (head != null) { + head.last = node; + node.next = head; + } else { + end = node; + } + head = node; + count++; + map.put(key, node); + } + } + + public static void main(String[] args) { + Q146 q146 = new Q146(2); + q146.put(2, 1); + q146.put(1, 1); + q146.put(2, 3); + q146.put(4, 1); + System.out.println(q146.get(1)); + System.out.println(q146.get(2)); +// q146.put(3, 2); +// System.out.println(q146.get(2)); +// System.out.println(q146.get(3)); +// q146.put(4, 4); +// System.out.println(q146.get(1)); +// System.out.println(q146.get(3)); +// System.out.println(q146.get(4)); + + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q326.java b/5.leetcode/src/com/fanxb/common/Q326.java new file mode 100644 index 0000000..5ff8868 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q326.java @@ -0,0 +1,12 @@ +package com.fanxb.common; + +/** + * @author fanxb + * @date 2021-09-23-上午11:27 + */ +public class Q326 { + + public boolean isPowerOfThree(int n) { + return n > 0 && 1162261467 % n == 0; + } +}