leetcode(Hot100)——栈与列表

1、用栈实现队列

一个栈进,一个栈出。

java 复制代码
class MyQueue {
    private LinkedList<Integer> stk1 = new LinkedList<>();
    private LinkedList<Integer> stk2 = new LinkedList<>();

    public MyQueue() {

    }
    
    public void push(int x) {
        stk1.push(x);
    }
    
    public int pop() {
        if(stk2.isEmpty()){
            while(!stk1.isEmpty()){
                stk2.push(stk1.pop());
            }
        }
        return stk2.pop();
    }
    
    public int peek() {
        if(stk2.isEmpty()){
            while(!stk1.isEmpty()){
                stk2.push(stk1.pop());
            }
        }
        return stk2.peek();
    }
    
    public boolean empty() {
        return stk1.isEmpty() && stk2.isEmpty();
    }
}

2、用队列实现栈

两个队列,一个队列负责存,第二个队列用于过滤数据。

java 复制代码
class MyStack {
    private LinkedList<Integer> que1 = new LinkedList<>();
    private LinkedList<Integer> que2 = new LinkedList<>();

    public MyStack() {

    }
    
    public void push(int x) {
        que1.addFirst(x);
    }
    
    public int pop() {
        int count = que1.size()-1;
        while(count > 0){
            que2.addFirst(que1.removeLast());
            count--;
        }
        int res = que1.removeLast();
        while(!que2.isEmpty()){
            que1.addFirst(que2.removeLast());
        }
        return res;
    }
    
    public int top() {
        return que1.getFirst();
    }
    
    public boolean empty() {
        return que1.isEmpty();
    }
}

3、前 K 个高频元素

比较朴素的做法是用一个map存储每个数字的出现频率,再从中选出k个最大频率的数字。本题也可以用优先队列的方式来做。

java 复制代码
class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        HashMap<Integer,Integer> map = new HashMap<>();
        List<Integer> ans = new ArrayList<>();
        for(int i=0; i<nums.length; i++){
            map.put(nums[i],map.getOrDefault(nums[i],0) + 1);
        }
        for(int i=0; i<k; i++){
            int max_freq = Integer.MIN_VALUE;
            int key = 0;
            for(Map.Entry<Integer,Integer> entry : map.entrySet()){
                if(entry.getValue() > max_freq){
                    max_freq = entry.getValue();
                    key = entry.getKey();
                }
            }
            ans.add(key);
            map.remove(key);
        }
        int[] res = new int[ans.size()];
        for(int i=0; i<res.length; i++){
            res[i] = ans.get(i);
        }
        return res;
    }
}

3、有效的括号

栈的经典应用,括号匹配 问题。 思路是构建一个临时栈,然后遍历字符串,如果是( { [ 就放入栈,如果不是就判断一下栈顶是否和{ ( [ 匹配,如果不匹配就返回false。 遍历完后,如果栈是空的则返回true。

java 复制代码
class Solution {
    public boolean isValid(String s) {
        LinkedList<Character> stk = new LinkedList<>();
        for(int i=0; i<s.length(); i++){
            if(stk.isEmpty()){
                if(s.charAt(i)==')' || s.charAt(i)==']' || s.charAt(i)=='}'){
                    return false;
                }
                stk.push(s.charAt(i));
            }
            else{
                if(stk.getFirst() == '('){
                    if(s.charAt(i) == ')'){
                        stk.pop();
                    }
                    else{
                        stk.push(s.charAt(i));
                    }
                }
                else if(stk.getFirst() == '['){
                    if(s.charAt(i) == ']'){
                        stk.pop();
                    }
                    else{
                        stk.push(s.charAt(i));
                    }
                }
                else if(stk.getFirst() == '{'){
                    if(s.charAt(i) == '}'){
                        stk.pop();
                    }
                    else{
                        stk.push(s.charAt(i));
                    }
                }
            }
        }
        if(stk.isEmpty()) return true;
        else return false;
    }
}
相关推荐
汉克老师14 分钟前
GESP7级C++考试语法知识(二、指数函数(3、综合练习)
c++·算法·数学建模·指数函数·gesp7级·复利
lulu121654407831 分钟前
OpenRouter Fusion 多模型融合架构深度拆解:预算级模型组团打平 Fable 5,多模型协作才是 AGI 的正确打开方式?
java·人工智能·架构·ai编程·agi
雨辰AI36 分钟前
生产级实测:SpringBoot3 + 达梦数据库接口从 200ms 优化至 20ms 完整调优指南
java·数据库·spring boot·后端·政务
林间码客1 小时前
04 ROC曲线与AUC:从零开始手动计算
大数据·人工智能·算法
Irissgwe1 小时前
map/set/multimap/multiset 的底层逻辑与实现
数据结构·c++·算法·二叉树·stl·c·红黑树
圣保罗的大教堂1 小时前
leetcode 2130. 链表最大孪生和 中等
leetcode
IronMurphy1 小时前
【算法五十八】23. 合并 K 个升序链表
数据结构·算法·链表
思茂信息1 小时前
CST软件基于液态金属开关的方向图可重构天线
服务器·算法·重构·cst·仿真软件·电磁仿真
(Charon)1 小时前
【C++ 面试高频:内存管理、RAII 和智能指针详解】
java·开发语言·word
凡人叶枫1 小时前
Effective C++ 条款39:明智而审慎地使用 private 继承
java·数据库·c++·嵌入式开发