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;
    }
}
相关推荐
大数据追光猿1 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!2 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉2 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生2 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴2 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing2 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财2 小时前
加油站(力扣134)
算法·leetcode·职场和发展
王老师青少年编程2 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
qy发大财2 小时前
柠檬水找零(力扣860)
算法·leetcode·职场和发展
瓦力的狗腿子2 小时前
Starlink卫星动力学系统仿真建模番外篇6-地球敏感器
算法·数学建模·simulink