博客记录-day136-力扣

68 阅读2分钟

一、力扣

1、基本计算器

224. 基本计算器

image.png

image.png

class Solution {  
    public int calculate(String s) {  
        // 创建一个栈用于存储操作符的符号  
        Deque<Integer> ops = new ArrayDeque<Integer>();  
        // 初始化栈,默认为正号  
        ops.push(1);  
        // 当前符号,初始为正  
        int sign = 1;  

        // 结果变量,存储计算结果  
        int ret = 0;  
        // 获取字符串的长度  
        int n = s.length();  
        // 用于遍历字符串的索引  
        int i = 0;  

        // 遍历整个字符串  
        while (i < n) {  
            // 如果当前字符是空格,则跳过  
            if (s.charAt(i) == ' ') {  
                i++;  
            // 如果当前字符是 '+',则设置当前符号为正  
            } else if (s.charAt(i) == '+') {  
                // 取栈顶符号,当前符号为正  
                sign = ops.peek();  
                i++;  
            // 如果当前字符是 '-',则设置当前符号为负  
            } else if (s.charAt(i) == '-') {  
                // 取栈顶符号,当前符号为负  
                sign = -ops.peek();  
                i++;  
            // 如果当前字符是 '(',则将当前符号压入栈中  
            } else if (s.charAt(i) == '(') {  
                ops.push(sign);  
                i++;  
            // 如果当前字符是 ')',则弹出栈顶符号  
            } else if (s.charAt(i) == ')') {  
                ops.pop();  
                i++;  
            // 如果当前是数字,开始 computing 数字  
            } else {  
                long num = 0; // 存储数字的临时变量  
                // 处理多位数字  
                while (i < n && Character.isDigit(s.charAt(i))) {  
                    num = num * 10 + s.charAt(i) - '0'; // 计算当前数字  
                    i++;  
                }  
                // 将计算结果加上当前符号乘以数字  
                ret += sign * num;  
            }  
        }  
        // 返回最终计算结果  
        return ret;  
    }  
}

2、打印

​1. 基础数组打印​

// 原始数组打印(需转换为字符串)
int[] arr = {1, 2, 3};
System.out.println(Arrays.toString(arr)); // [1, 2, 3]

// 二维数组
int[][] matrix = {{1,2}, {3,4}};
System.out.println(Arrays.deepToString(matrix)); // [[1, 2], [3, 4]]

​2. 集合框架打印​

​List 打印​
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
System.out.println(list); // [A, B](自动调用 toString())

// 遍历打印
for (String s : list) {
    System.out.print(s + " "); // A B 
}
​Set 打印​

Set<Integer> set = new HashSet<>();
set.add(3);
set.add(1);
set.add(2);
System.out.println(set); // 无序输出,如 [1, 2, 3]

// TreeSet 有序打印
Set<Integer> treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [1, 2, 3]
​Map 打印​
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
System.out.println(map); // {Alice=25, Bob=30}(无序)

// TreeMap 有序打印
Map<String, Integer> treeMap = new TreeMap<>(map);
System.out.println(treeMap); // {Alice=25, Bob=30}

// 遍历键值对
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

ArrayDeque 打印​

//直接输出即可
System.out.println(queue);

// 转为 ArrayList 打印
System.out.println(new ArrayList<>(deque)); // 输出 [1, 2, 3]

// 转为 LinkedList 打印
System.out.println(new LinkedList<>(deque)); // 输出 [1, 2, 3]

3、用栈实现队列

232. 用栈实现队列

image.png

image.png

class MyQueue {
    ArrayDeque<Integer> input;
    ArrayDeque<Integer> output;
    public MyQueue() {
        input=new ArrayDeque<>();
        output=new ArrayDeque<>();
    }
    
    public void push(int x) {
        input.push(x);
    }
    
    public int pop() {
        if(!output.isEmpty()){
            return output.poll();
        }else{
            while(!input.isEmpty()){
                output.push(input.poll());
            }
            return output.poll();
        }
    }
    
    public int peek() {
        if(output.isEmpty()){
            while(!input.isEmpty()){
                output.push(input.poll());
            }
        }
        return output.peek();
    }
    
    public boolean empty() {
        return input.isEmpty()&&output.isEmpty();
    }
}

4、用队列实现栈

225. 用队列实现栈

image.png

image.png

class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue1.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue1.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }
}