第32天--算法(Leetcode 136,138,139,140,141,146)
136.只出现一次的数字
public int singleNumber(int[] nums) {
int ans = 0;
for(int i : nums) {
ans ^= i;
}
return ans;
}
138.复制带随机指针的链表
public Node copyRandomList(Node head) {
if(head == null) {
return null;
}
Node cur = head;
while(cur != null) {
Node temp = new Node(cur.val);
temp.next = cur.next;
cur.next = temp;
cur = temp.next;
}
cur = head;
while(cur != null) {
cur.next.random = cur.random == null ? null : cur.random.next;
cur = cur.next.next;
}
cur = head;
Node res = head.next;
Node temp = res;
while(temp.next != null) {
cur.next = temp.next;
cur = cur.next;
temp.next = cur.next;
temp = temp.next;
}
cur.next = null;
return res;
}
139.单词拆分
public boolean wordBreak(String s, List<String> wordDict) {
HashSet<String> hs = new HashSet<>(wordDict);
int N = s.length();
boolean dp[] = new boolean[N + 1];
dp[N] = true;
for(int index = N - 1;index >= 0;index --) {
for(int j = index;j < s.length();j ++) {
if(hs.contains(s.substring(index,j + 1))) {
if(dp[j + 1]) {
dp[index] = true;
continue;
}
}
}
}
return dp[0];
}
140.单词拆分2
public List<String> wordBreak(String s, List<String> wordDict) {
HashSet<String> hs = new HashSet<>(wordDict);
int N = s.length();
boolean dp[] = new boolean[N + 1];
dp[N] = true;
List<String> res = new ArrayList<>();
StringBuilder path = new StringBuilder();
process(s,0,hs,path,res);
return res;
}
public void process(String s,int index,HashSet<String> hs,StringBuilder path,List<String> res) {
if(index == s.length()) {
res.add(path.toString().substring(0,path.length() - 1));
}else {
for(int j = index;j < s.length();j ++) {
StringBuilder rest = new StringBuilder(path);
if(hs.contains(s.substring(index,j + 1))) {
path.append(s.substring(index,j + 1)).append(" ");
process(s,j + 1,hs,path,res);
}
path = rest;
}
}
}
141.环形链表
public boolean hasCycle(ListNode head) {
return process(head) != null;
}
public ListNode process(ListNode head) {
if(head == null || head.next == null || head.next.next == null) {
return null;
}
ListNode fast = head.next.next;
ListNode slow = head.next;
while(slow != fast) {
if(fast.next == null || fast.next.next == null) {
return null;
}
fast = fast.next.next;
slow = slow.next;
}
fast = head;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
146.LRU缓存
private MyCache<Integer, Integer> cache;
public LRUCache(int capacity) {
cache = new MyCache<>(capacity);
}
public int get(int key) {
Integer ans = cache.get(key);
return ans == null ? -1 : ans;
}
public void put(int key, int value) {
cache.set(key, value);
}
public static class Node<K, V> {
public K key;
public V value;
public Node<K, V> last;
public Node<K, V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
public static class NodeDoubleLinkedList<K, V> {
private Node<K, V> head;
private Node<K, V> tail;
public NodeDoubleLinkedList() {
head = null;
tail = null;
}
public void addNode(Node<K, V> newNode) {
if (newNode == null) {
return;
}
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
newNode.last = tail;
tail = newNode;
}
}
public void moveNodeToTail(Node<K, V> node) {
if (tail == node) {
return;
}
// node 不是尾巴
if (head == node) {
head = node.next;
head.last = null;
} else {
node.last.next = node.next;
node.next.last = node.last;
}
node.last = tail;
node.next = null;
tail.next = node;
tail = node;
}
public Node<K, V> removeHead() {
if (head == null) {
return null;
}
Node<K, V> res = head;
if (head == tail) { // 链表中只有一个节点的时候
head = null;
tail = null;
} else {
head = res.next;
res.next = null;
head.last = null;
}
return res;
}
}
public static class MyCache<K, V> {
private HashMap<K, Node<K, V>> keyNodeMap;
private NodeDoubleLinkedList<K, V> nodeList;
private final int capacity;
public MyCache(int cap) {
if (cap < 1) {
throw new RuntimeException("should be more than 0.");
}
keyNodeMap = new HashMap<K, Node<K, V>>();
nodeList = new NodeDoubleLinkedList<K, V>();
capacity = cap;
}
public V get(K key) {
if (keyNodeMap.containsKey(key)) {
Node<K, V> res = keyNodeMap.get(key);
nodeList.moveNodeToTail(res);
return res.value;
}
return null;
}
public void set(K key, V value) {
if (keyNodeMap.containsKey(key)) {
Node<K, V> node = keyNodeMap.get(key);
node.value = value;
nodeList.moveNodeToTail(node);
} else {
if (keyNodeMap.size() == capacity) {
removeMostUnusedCache();
}
Node<K, V> newNode = new Node<K, V>(key, value);
keyNodeMap.put(key, newNode);
nodeList.addNode(newNode);
}
}
private void removeMostUnusedCache() {
Node<K, V> removeNode = nodeList.removeHead();
keyNodeMap.remove(removeNode.key);
}
}