LeetCode——日常刷题(二)

第一题:155. 最小栈

 代码实现

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. 有效的括号

代码实现

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. 用队列实现栈

代码实现 

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. 用栈实现队列

代码实现

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. 设计循环队列

代码实现

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. 重新排列数组

代码实现

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. 数组串联

 代码实现

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. 基于排列构建数组

代码实现

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. 一维数组的动态和

 代码实现

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. 左旋转字符串

 代码实现:

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();
    }
}

评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员-idea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值