每日十道算法

1、零钱兑换

给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。

计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 -1 。

你可以认为每种硬币的数量是无限的。

 

 

class Solution {
    public int coinChange(int[] coins, int amount) {
        int max = Integer.MAX_VALUE;
        int[] dp = new int[amount + 1];
        //初始化dp数组为最大值
        for (int j = 0; j < dp.length; j++) {
            dp[j] = max;
        }
        //当金额为0时需要的硬币数目为0
        dp[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            //正序遍历:完全背包每个硬币可以选择多次
            for (int j = coins[i]; j <= amount; j++) {
                //只有dp[j-coins[i]]不是初始最大值时,该位才有选择的必要
                if (dp[j - coins[i]] != max) {
                    //选择硬币数目最小的情况
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }
        return dp[amount] == max ? -1 : dp[amount];
    }
}

2、零钱兑换 II

 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount
表示总金额。

请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。

假设每一种面额的硬币有无限个。 

题目数据保证结果符合 32 位带符号整数。

 

 

class Solution {
    public int change(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        for(int i = 0; i < coins.length; i++){
            for(int j = coins[i]; j <= amount;j++){
                dp[j] += dp[j -coins[i]];
            }
        }
        return dp[amount];
    }
}

 

3、有效的括号

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

 

 

import java.util.*;
class Solution {

    public boolean isValid(String s) {
        if(s.isEmpty()){
            return true;
        }
            
        Stack<Character> stack=new Stack<Character>();
        for(char c:s.toCharArray()){
            if(c=='(')
                stack.push(')');
            else if(c=='{')
                stack.push('}');
            else if(c=='[')
                stack.push(']');
            else if(stack.empty()||c!=stack.pop())
                return false;
        }
        if(stack.empty()){
            return true;
        }            
        return false;
    }
}

 

4、包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。

 

class MinStack {

    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> res = new Stack<>();
    /** initialize your data structure here. */
    public MinStack() {

    }
    
    public void push(int x) {
        if(stack.isEmpty()){
            stack.push(x);
            res.push(x);
            return;
        }
        stack.push(x);
        int topVal = res.peek();
        if(x > topVal){
            res.push(topVal);
        }else{
            res.push(x);
        }
    }
    
    public void pop() {
        stack.pop();
        res.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int min() {
        return res.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */

 

5、用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类:

void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

 

import java.util.*;
class MyStack {
    Queue<Integer> queue = null;
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.offer(x);
    }
    
    public int pop() {
      int size = queue.size();
      size--;
      while(size > 0){
          queue.offer(queue.poll());
          size--;
      }
      return queue.poll();
    }
    
    public int top() {
      int size = queue.size();
      size--;
      while(size > 0){
          queue.offer(queue.poll());
          size--;
      }
      int temp = queue.poll();
      queue.offer(temp);
      return temp;
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

6、用栈实现队列

 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false

 

import java.util.*;
class MyQueue {

    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();
    public MyQueue() {

    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if(stack2.empty()){
            while(!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
    
    public int peek() {
        if(stack2.empty()){
            while(!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
       return stack2.peek();
    }
    
    public boolean empty() {
        if(stack2.empty()){
            while(!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

 

7、后缀表达式

 根据 逆波兰表示法,求该后缀表达式的计算结果。

有效的算符包括 +、-、*、/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。

 

 

 

import java.util.*;
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens){
            if("+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s)){
                int b = stack.pop();
                int a = stack.pop();
                if("+".equals(s)) {
                    stack.push(a + b);
                } else if ("-".equals(s)) {
                    stack.push(a - b);
                } else if ("*".equals(s)) {
                    stack.push(a * b);
                } else if ("/".equals(s)) {
                    stack.push(a / b);
                }

            }else{
                stack.push(Integer.parseInt(s));
            }
        }
        return stack.pop();
    }
}

8、滑动窗口最大值

 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回滑动窗口中的最大值。

 

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums == null || nums.length < 2) return nums;
        // 双向队列 保存当前窗口最大值的数组位置 保证队列中数组位置的数值按从大到小排序
        LinkedList<Integer> queue = new LinkedList();
        // 结果数组
        int[] result = new int[nums.length-k+1];
        // 遍历nums数组
        for(int i = 0;i < nums.length;i++){
            // 保证从大到小 如果前面数小则需要依次弹出,直至满足要求
            while(!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]){
                queue.pollLast();
            }
            // 添加当前值对应的数组下标
            queue.addLast(i);
            // 判断当前队列中队首的值是否有效
            if(queue.peek() <= i-k){
                queue.poll();   
            } 
            // 当窗口长度为k时 保存当前窗口中最大值
            if(i+1 >= k){
                result[i+1-k] = nums[queue.peek()];
            }
        }
        return result;
    }
}

 

9、圆圈中最后剩下的数字

0,1,···,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字(删除后从下一个数字开始计数)。求出这个圆圈里剩下的最后一个数字。

例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。

 

class Solution {
    public int lastRemaining(int n, int m) {
        return f(n, m);
    }

    public int f(int n, int m) {
        if (n == 1) {
            return 0;
        }
        int x = f(n - 1, m);
        return (m + x) % n;
    }
}



class Solution {
    public int lastRemaining(int n, int m) {
        int res = 0;
        for (int i = 2; i <= n; i++) {
            res = (res + m) % i;
        }
        return res;
    }
}

 

10、LRU 缓存

请你设计并实现一个满足  LRU (最近最少使用) 缓存 约束的数据结构。实现LRUCache类:

时间复杂度:$O(1)
空间复杂度:$O(n) 

class LRUCache{
	class Node{//双向链表节点
		int key;
		int value;
		Node prev;
		Node next;
		
		public Node() {
		}
		public Node(int key, int value) {
			this.key = key;
			this.value = value;
		}
	}


	private int capacity;
	private Map<Integer, Node> map;
	private DoubleLinkedList doubleLinkedList;
	
	
	public LRUCache(int capacity) {
		this.capacity = capacity;
		map = new HashMap();
		doubleLinkedList = new DoubleLinkedList();
	}    
	
    //新的插入头部,旧的从尾部移除
	class DoubleLinkedList{
		Node head;
		Node tail;
		
		public DoubleLinkedList() {
            //头尾哨兵节点
			this.head = new Node();
			this.tail = new Node();
			this.head.next = this.tail;
			this.tail.prev = this.head;
		}
		
		public void addHead(Node node) {
			node.next = this.head.next;
			node.prev = this.head;
			this.head.next.prev = node;
			this.head.next = node;
		}
		
		public void removeNode(Node node) {
			node.prev.next = node.next;
			node.next.prev = node.prev;
			node.prev = null;
			node.next = null;
		}
		
		public Node getLast() {
			if(this.tail.prev == this.head)
				return null;
			return this.tail.prev;
		}

	}

	public int get(int key) {
		if(!map.containsKey(key)) {
			return -1;
		}
		
		Node node = map.get(key);
        
        //更新节点位置,将节点移置链表头
		doubleLinkedList.removeNode(node);
		doubleLinkedList.addHead(node);
		return node.value;
	}
	
	public void put(int key, int value) {
		
		if(map.containsKey(key)) {
			Node node = map.get(key);
			node.value = value;
			map.put(key, node);
			doubleLinkedList.removeNode(node);
			doubleLinkedList.addHead(node);
		}else {	
			if(map.size() == capacity) {//已达到最大容量了,把旧的移除,让新的进来
				Node lastNode = doubleLinkedList.getLast();
				map.remove(lastNode.key);//node.key主要用处,反向连接map
				doubleLinkedList.removeNode(lastNode);
			}
			
			Node newNode = new Node(key, value);
			map.put(key, newNode);
			doubleLinkedList.addHead(newNode);
		}
	}	
}

 1.通过双向链表来实现,新数据插入到链表头部
2.每当缓存命中(即缓存数据被访问),则将数据移到链表头部
3.当链表满的时候,将链表尾部的数据丢弃。
LinkedHashMap: HashMap和双向链表合二为一印是 LinkedHashMap
HashMap是无序的, LinkedHashMap通过维护一个额外的双向链表保证了迭代顺序。该迭代顺序可以是插入顺序(默认),也可以是访问顺序。

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache {

    private LinkedHashMap<Integer, Integer> cache;

    public LRUCache(int capacity) {
        cache = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true){
            @Override
            protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
                return size() > capacity;
            }
        };
    }

    public int get(int key) {
        return cache.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        cache.put(key, value);
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zpeien

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值