【剑指 Offer】第 25 天

105 阅读2分钟

Offer 驾到,掘友接招!我正在参与2022春招系列活动-刷题打卡任务,点击查看活动详情

一、前言

刷题啊!!!

开始刷 “剑指 Offer” 31天。刷完时间:2022.3.6 ~ 2022.3.20。

Tips

// List<Integer> 转换为 数组
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
 
int[] primitive = list.stream()
                    .mapToInt(Integer::intValue)
                    .toArray();



二、题目

题目:

  • 顺时针打印矩阵
  • 栈的压入、弹出序列

(1)剑指 Offer 29. 顺时针打印矩阵

题目描述


输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

限制:

  • 0 <= matrix.length <= 100
  • 0 <= matrix[i].length <= 100

题解


思路:

  • 考虑好边界
  • 循环:
    1. 从左到右
    2. 从上到下
    3. 从右到左
    4. 从下到上

AC 代码如下:

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0] == null) return new int[]{};

        int left = 0, right = matrix[0].length, top = 0, bottom = matrix.length;

        List<Integer> result = new ArrayList<>();

        while (left < right ) {

            // 从左到右
            for (int i = left; i < right; ++i) result.add(matrix[top][i]);
            if (++top >= bottom) break;

            // 从上到下
            for (int i = top; i < bottom; ++i) result.add(matrix[i][right - 1]);
            if (--right <= left) break;

            // 从右到左
            for (int i = right - 1; i >= left; --i) result.add(matrix[bottom - 1][i]);
            if (--bottom <= top) break;

            // 从下到上
            for (int i = bottom - 1; i >= top; --i) result.add(matrix[i][left]);
            if (++left >= right) break;
        }
        return result.stream()
                .mapToInt(Integer::intValue)
                .toArray();
    }
}



(2)剑指 Offer 31. 栈的压入、弹出序列

题目描述


输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。

示例 1:

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:

输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。

提示:

  • 0 <= pushed.length == popped.length <= 1000
  • 0 <= pushed[i], popped[i] < 1000
  • pushedpopped 的排列。

题解


发现我写的好复杂。0.0

思路如下:

  1. 初始化:栈、队列(将 pushed 数组放入)
  2. 遍历 popped 数组,当前元素 value
    • 队列中首个元素等于 value,则为合法,继续循环
    • 栈中首个元素等于 value,则为合法,继续循环
    • 在队列中查询,并把元素放入栈中

AC 代码如下:

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        Queue<Integer> queue = new LinkedList<>();

        for (int num : pushed) queue.add(num);

        for (int value : popped) {

            if (!queue.isEmpty() && queue.peek() == value) {

                queue.poll();
                continue;
            }

            if (!stack.isEmpty() && stack.peek() == value) {

                stack.pop();
                continue;
            }

            while (!queue.isEmpty() && queue.peek() != value) {

                stack.push(queue.poll());
            }

            if (!queue.isEmpty() && queue.peek() == value) {

                queue.poll();
                continue;
            }

            while (!stack.isEmpty() && stack.peek() != value) {
                stack.pop();
            }

            if (stack.isEmpty() || stack.peek() != value) return false;

            stack.pop();
        }
        return true;
    }
}

简化版:只需判断栈是否为空。

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for(int num : pushed) {
            // num 入栈
            stack.push(num); 
            // 循环判断与出栈
            while(!stack.isEmpty() && stack.peek() == popped[i]) { 
                stack.pop();
                i++; // 移动指针
            }
        }
        return stack.isEmpty();
    }
}