代码随想录四刷day10

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


其实很多算法题目主要是对知识点的考察和教学意义远大于其工程实践的意义,所以面试题也是这样

一、力扣977. 有序数组的平方

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        for(int i = 0, j = nums.length-1, k = j; i <= j ;){
            if(nums[i] * nums[i] <= nums[j] * nums[j]){
                res[k --] = nums[j] * nums[j];
                j --;
            }else{
                res[k --] = nums[i] * nums[i];
                i ++;
            }
        }
        return res;
    }
}

二、力扣209. 长度最小的子数组

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;
        int left = 0, right = 0, sum = 0;
        while(right < nums.length){
            sum += nums[right];
            right ++;
            while(sum >= target){
                res = Math.min(res,right - left);
                sum -= nums[left];
                left ++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

三、力扣232. 用栈实现队列

class MyQueue {
    Deque<Integer> deq1 = new LinkedList<>();
    Deque<Integer> deq2 = new LinkedList<>();
    public MyQueue() {

    }
    
    public void push(int x) {
        deq1.offerLast(x);
    }
    
    public int pop() {
        if(deq2.isEmpty()){
            while(!deq1.isEmpty()){
                int temp = deq1.pollLast();
                deq2.offerLast(temp);
            }
        }
        return deq2.pollLast();
    }
    
    public int peek() {
        if(deq2.isEmpty()){
            while(!deq1.isEmpty()){
                int temp = deq1.pollLast();
                deq2.offerLast(temp);
            }
        }
        return deq2.peekLast();
    }
    
    public boolean empty() {
        return deq1.isEmpty() && deq2.isEmpty();
    }
}

/**
 * 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();
 */

四、力扣225. 用队列实现栈

class MyStack {
    Deque<Integer> deq1 = new LinkedList<>();
    Deque<Integer> deq2 = new LinkedList<>();

    public MyStack() {

    }
    
    public void push(int x) {
        deq1.offerLast(x);
    }
    
    public int pop() {
        if(deq1.isEmpty()){
            while(deq2.size() > 1){
                int temp = deq2.pollFirst();
                deq1.offerLast(temp);
            }
            return deq2.pollFirst();
        }
        while(deq1.size() > 1){
            int temp = deq1.pollFirst();
            deq2.offerLast(temp);
        }
        return deq1.pollFirst();
    }
    
    public int top() {
        if(deq1.isEmpty()){
            while(deq2.size() > 1){
                int temp = deq2.pollFirst();
                deq1.offerLast(temp);
            }
            int res = deq2.pollFirst();
            deq1.offerLast(res);
            return res;
        }
        while(deq1.size() > 1){
            int temp = deq1.pollFirst();
            deq2.offerLast(temp);
        }
        int res = deq1.pollFirst();
        deq2.offerLast(res);
        return res;
    }
    
    public boolean empty() {
        return deq1.isEmpty() && deq2.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();
 */
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乱世在摸鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值