代码随想录Day8

目录

1、459.重复的子字符串

2、232.用栈实现队列

3、225. 用队列实现栈

4、20. 有效的括号

5、1047. 删除字符串中的所有相邻重复项

6、150. 逆波兰表达式求值


1、459.重复的子字符串

巧用kmp的next数组

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int[] next = new int[s.length()];
        int j = 0;
        next[0] = 0;
        for (int i = 1; i < s.length(); ++i)
        {
            while(j > 0 && s.charAt(j) != s.charAt(i))
            {
                j = next[j - 1];
            }
            if(s.charAt(j) == s.charAt(i))
            {
                ++j;
            }
            next[i] = j;
        }
        if (next[next.length - 1] != 0 && next.length % (next.length - next[next.length - 1]) == 0)
            return true;
        else
            return false; 
    }
 
}

2、232.用栈实现队列

需要两个栈

class MyQueue {
    Stack<Integer> inStack;
    Stack<Integer> outStack;
    public MyQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }
    
    public void push(int x) {
        inStack.push(x);
    }
    
    public int pop() {
        in2out();
        return outStack.pop();
    }
    
    public int peek() {
        in2out();
        return outStack.peek();
    }
    
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    public void in2out()
    {
        if(!outStack.isEmpty())
            return;
        while(!inStack.isEmpty())
            outStack.push(inStack.pop());
    }
}

3、225. 用队列实现栈

可以用一个队列实现。

注意:LinkedList实现了Queue接口。

Queue接口定义的方法:

  1. add(element):将指定的元素插入到队尾,如果成功则返回 true,如果队列已满则抛出异常。
  2. offer(element):将指定的元素插入到队尾,如果成功则返回 true,如果队列已满则返回 false。
  3. remove():移除并返回队首的元素,如果队列为空则抛出异常。
  4. poll():移除并返回队首的元素,如果队列为空则返回 null。
  5. element():返回队首的元素,但不移除它,如果队列为空则抛出异常。
  6. peek():返回队首的元素,但不移除它,如果队列为空则返回 null。
class MyStack {
    Queue<Integer> que;
    public MyStack() {
        que = new LinkedList<>();
    }
    
    public void push(int x) {
        que.add(x);
    }
    
    public int pop() {
        move();
        return que.poll();
    }
    
    public int top() {
        move();
        int res = que.peek();
        que.add(que.poll());
        return res;
    }
    
    public boolean empty() {
        return que.size() == 0 ? true : false;
    }

    public void move()
    {
        int size = que.size();
        for(int i = 0; i < size - 1; ++i)
        {
            que.add(que.poll());
        }
    }
}

4、20. 有效的括号

三种情况:(1)左括号多了-->最后栈不为空(2)右括号多了-->栈空了但还没有匹配完(3)没配上

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length();++i){
            char ch = s.charAt(i);
            switch(ch)
            {
                case '(':
                    stack.push(')');
                    break;
                case '{':
                    stack.push('}');
                    break;
                case '[':
                    stack.push(']');
                    break; 
                default:
                    if(stack.isEmpty() || ch != stack.pop())
                        return false;                                       
            }
        }
        if(stack.isEmpty())
            return true;
        else
            return false;
    }
}

5、1047. 删除字符串中的所有相邻重复项

这不就是祖玛么~

StringBuffer用法:

1、增:

public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

2、删:

public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回字符串缓冲区本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回字符串缓冲区本身

3、替换:
public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 也是返回字符串缓冲区本身

4、反转:
public StringBuffer reverse():字符串反转 也是返回字符串缓冲区本身

5、截取:

public String substring(int start):从指定位置截取到末尾
public String substring(int start,int end):截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

class Solution {
    public String removeDuplicates(String s) {
        StringBuffer res = new StringBuffer();
        int size = -1;
        for(int i = 0; i < s.length(); ++i)
        {
            char ch = s.charAt(i);
            if(size != -1 && res.charAt(size) == ch)
            {
                res.deleteCharAt(size);
                --size;
            }
            else
            {
                res.append(ch);
                ++size;
            }
        }
        return res.toString();

    }
}

6、150. 逆波兰表达式求值

遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。

class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for(int i = 0; i < tokens.length; ++i)
        {
            switch(tokens[i])
            {
                case "+":
                    stack.push(stack.pop() + stack.pop());
                    break;
                case "-":
                    stack.push(-stack.pop() + stack.pop());
                    break;
                case "*":
                    stack.push(stack.pop() * stack.pop());
                    break;
                case "/":
                    int temp1 = stack.pop();
                    int temp2 = stack.pop();
                    stack.push(temp2 / temp1);
                    break;
                default:
                    stack.push(Integer.valueOf(tokens[i]));
                    break;
            }
        }
        return stack.pop();
    }
}

  • 24
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值