class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
List<Boolean> list = new ArrayList<>();
int max = 0;
for(int i:candies){
max = Math.max(max,i);
}
extraCandies = max - extraCandies;
for(int candy:candies){
list.add(candy>=extraCandies);
}
return list;
}
}
class LRUCache {
class DLinkedNode {
int key;
int value;
DLinkedNode prev;
DLinkedNode next;
public DLinkedNode(){}
public DLinkedNode(int key,int value){
this.key = key;
this.value = value;
}
}
public void addNode(DLinkedNode node){
node.prev = head;
node.next = head.next;
head.next.prev = node;
head.next = node;
}
public void deleteNode(DLinkedNode node){
DLinkedNode prev = node.prev;
DLinkedNode next = node.next;
prev.next = next;
next.prev = prev;
}
public void moveToHead(DLinkedNode node){
this.deleteNode(node);
this.addNode(node);
}
public DLinkedNode popTail(){
DLinkedNode node = tail.prev;
this.deleteNode(node);
return node;
}
private Map<Integer,DLinkedNode> cache = new HashMap<>();
private int count;
private int capacity;
private DLinkedNode head,tail;
public LRUCache(int capacity) {
this.count = 0;
this.capacity = capacity;
head = new DLinkedNode();
head.prev = null;
tail = new DLinkedNode();
tail.next = null;
head.next = tail;
tail.prev = head;
}
public int get(int key) {
DLinkedNode node = cache.get(key);
if(node==null){
return -1;
}
this.moveToHead(node);
return node.value;
}
public void put(int key, int value) {
DLinkedNode node = cache.get(key);
if(node==null){
DLinkedNode newHead = new DLinkedNode();
newHead.key = key;
newHead.value = value;
cache.put(key,newHead);
this.addNode(newHead);
count++;
//如果count大于capacity,则说明缓存容量达到上限。
if(count>capacity){
//移除最近最少使用的结点(即链表的最好一个结点)
DLinkedNode tail = this.popTail();
//删除map中对应的数据
this.cache.remove(tail.key);
count--;
}
}else{
node.value = value;
this.moveToHead(node);
}
}
}
class Solution {
public int pathSum(TreeNode root, int sum) {
if(root==null){
return 0;
}
return helper(root,sum) + pathSum(root.left,sum) + pathSum(root.right,sum);
}
private int helper(TreeNode node,int sum){
if(null==node){
return 0;
}
sum -= node.val;
int count = sum==0?1:0;
count += helper(node.left,sum);
count += helper(node.right,sum);
return count;
}
}
class Solution {
public int pathSum(TreeNode root, int sum) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
return helper(root, map, sum, 0);
}
private int helper(TreeNode node, Map<Integer, Integer> prefixSum, int sum, int curSum) {
if (node == null) {
return 0;
}
curSum += node.val;
int count = prefixSum.getOrDefault(curSum - sum, 0);
prefixSum.put(curSum, prefixSum.getOrDefault(curSum, 0) + 1);
count += helper(node.left, prefixSum, sum, curSum);
count += helper(node.right, prefixSum, sum, curSum);
prefixSum.put(curSum, prefixSum.getOrDefault(curSum, 0) - 1);
return count;
}
}
class Solution {
int res = 0;
public int pathSum(TreeNode root, int sum) {
int dep = depth(root);
int[] paths = new int[dep];
pathSum(root, sum, 0, paths);
return res;
}
public void pathSum(TreeNode root, int sum, int level, int[] paths) {
if (root == null) {
return;
}
paths[level] = root.val;
int t = 0;
for (int i = level; i >= 0; i--) {
t += paths[i];
if (t == sum) {
res += 1;
}
}
pathSum(root.left, sum, level + 1, paths);
pathSum(root.right, sum, level + 1, paths);
}
public int depth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}
你知道的越多,你不知道的越多。