LeetCode——日常刷题(二)

简介: LeetCode——日常刷题(二)

一题:155. 最小栈

image.png

代码实现

class MinStack {
   //栈stack用来保存数值
   //栈minStack用来保存最小值
  Stack<Integer> stack;
  Stack<Integer> minStack;
  public MinStack() {
    stack = new Stack<>();
    minStack = new Stack<>();
  }
  public void push(int x) {
    stack.push(x);
    if (minStack.isEmpty() || x <= minStack.peek()) {
      minStack.push(x);
    }
  }
  public void pop() {
    if (stack.pop().equals(minStack.peek())) {
      minStack.pop();
    }
  }
  public int top() {
    return stack.peek();
  }
  public int getMin() {
    return minStack.peek();
  }
}

第二题:20. 有效的括号

image.png

代码实现

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for(int i=0;i<s.length();i++){
           char c= s.charAt(i);
            //左括号进栈
        if(c=='('||c=='{'||c=='['){
               stack.push(c);
           }
           else{
               if(stack.isEmpty()){return false;}
               Character topChar=stack.pop();
            //右括号进行判断,若不同类型返回false;
                if(c==')'&&topChar!='('){
                    return false;
                }
                if(c==']'&&topChar!='['){
                    return false;
                }
                if(c=='}'&&topChar!='{'){
                    return false;
                }
           }
        }
        return stack.isEmpty();
    }
}

第三题:225. 用队列实现栈

image.png

代码实现

class MyStack {
  //用两个队列来实现栈
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }
    public void push(int x) {
        //当为空的时候,插入x
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    public int pop() {
        return queue1.poll();
    }
    public int top() {
        return queue1.peek();
    }
    public boolean empty() {
        return queue1.isEmpty();
    }
}

第四题:232. 用栈实现队列

image.png

代码实现

1.class CQueue {
    //创建两个栈
    Stack<Integer> stack1;
    Stack<Integer> stack2;
    public CQueue() {
        stack1=new Stack<Integer>();
        stack2=new Stack<Integer>();
    }
    public void appendTail(int value) {
        //入队直接压入栈中
        stack1.push(value);
    }
    public int deleteHead() {
    if(stack2.empty()){
        while(!stack1.empty()){
             stack2.push(stack1.pop());
        }
    }
    if(stack2.empty()){
        return -1;
    }
    return stack2.pop();
}
}

第五题:622. 设计循环队列

image.png

代码实现

class MyCircularQueue {
    final int[] array;
    final int k;
    int left;//指向队首元素
    int right;
    public MyCircularQueue(int k) {
        this.array=new int[k];
        this.k=k;
        Arrays.fill(array,-1);
        left=k;
        right=k;
    }
    public boolean enQueue(int value) {
        if(isFull())return false;
        else{
            array[right%k]=value;
            right++;
            return true;
        }
    }
    public boolean deQueue() {
        if(isEmpty())return false;
        else{
            array[left%k]=-1;
            left++;
            return true;
        }
    }
    public int Front() {
        if(isEmpty())return -1;
        return array[left%k];
    }
    public int Rear() {
        if(isEmpty())return -1;
        //(right-1)%k 防止数组越界 right%k-1会越界
        else return array[(right-1)%k];
    }
    public boolean isEmpty() {
        if(left==right)return true;
        return false;
    }
    public boolean isFull() {
        if(right-left==k)return true;
        return false;
    }
}

第六题:1470. 重新排列数组

image.png

代码实现

class Solution {
    public int[] shuffle(int[] nums, int n) {
        int[] arr=new int[2*n];
        for(int i=0;i<n;i++){
            arr[2*i]=nums[i];
            arr[2*i+1]=nums[i+n];
        }
        return arr;
    }
}

第七题:1929. 数组串联

image.png

代码实现

class Solution {
    public int[] getConcatenation(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n * 2];
        for(int i = 0; i < n; i++){
            ans[i] = nums[i];
            ans[i + n] = nums[i];
        }
        return ans;
    }
}

第八题:1920. 基于排列构建数组

image.png

代码实现

class Solution {
    public int[] buildArray(int[] nums) {
        int[] arr=new int[nums.length];
        for(int i=0;i<nums.length;i++){
            arr[i]=nums[nums[i]];
        }
        return arr;
    }
}

第九题:1480. 一维数组的动态和

image.png

代码实现

class Solution {
    public int[] runningSum(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        int sum=0;
        for (int i = 0; i < n; i++) {
             sum=sum+ nums[i];
             ans[i]=s;
        }
        return ans;
    }
}

十题:剑指 Offer 58 - II. 左旋转字符串

image.png

代码实现:

class Solution {
    public String reverseLeftWords(String s, int n) {
        StringBuilder ans=new StringBuilder();
        for(int i=n;i<s.length();i++){
            ans.append(s.charAt(i));
        }
        for(int i=0;i<n;i++){
            ans.append(s.charAt(i));
        }
        return ans.toString();
    }
}
相关文章
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
41 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
35 4
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
73 2
|
1月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
36 7
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
17 4
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
40 5
|
1月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
36 3
|
1月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
15 3
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
28 3
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Leetcode题目"剑指 Offer 21. 调整数组顺序使奇数位于偶数前面"的两种Python解决方案,一种是使用双端队列调整数组顺序,另一种是使用双指针法将奇数移到数组前半部分,偶数移到后半部分。
21 4