剑指offer[29、30]

14 篇文章 0 订阅
9 篇文章 0 订阅

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

题目

在这里插入图片描述

思路

通过可视化限制四个方向运动的边界线
在这里插入图片描述

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right.
            if(++t > b) break;
            for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom.
            if(l > --r) break;
            for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left.
            if(t > --b) break;
            for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top.
            if(++l > r) break;
        }
        return res;
    }
}
class Solution:
    def spiralOrder(self, matrix:[[int]]) -> [int]:
        if not matrix: return []
        l, r, t, b, res = 0, len(matrix[0]) - 1, 0, len(matrix) - 1, []
        while True:
            for i in range(l, r + 1): res.append(matrix[t][i]) # left to right
            t += 1
            if t > b: break
            for i in range(t, b + 1): res.append(matrix[i][r]) # top to bottom
            r -= 1
            if l > r: break
            for i in range(r, l - 1, -1): res.append(matrix[b][i]) # right to left
            b -= 1
            if t > b: break
            for i in range(b, t - 1, -1): res.append(matrix[i][l]) # bottom to top
            l += 1
            if l > r: break
        return res

剑指 Offer 30. 包含min函数的栈

题目

在这里插入图片描述

思路

使用两个栈来完成任务,A栈正常存储数据,B仅用于存储无绝对顺序的最小值,始终保持B的顶点存储着A的最小值(保证可以有O(1)的时间复杂度);

代码

class MinStack {
    Stack<Integer> A,B;//全局变量其他的方法均可以调用这两个栈
    /** initialize your data structure here. */
    public MinStack() {
        A=new Stack<Integer>();
        B=new Stack<Integer>();
    }
    public void push(int x) {
        A.add(x);
        if(B.empty()||B.peek()>=x) B.add(x);//注意对于与B顶端存储的值相同的x也需要押送进B栈中,因为要与pop的方法相互对应。如果pop出一个最小值的话,B中应该还存有另一个相等的最小值
    }    
    public void pop() {
        // 注意此时A.pop()方法在if条件中,但是也相当于运行了A.pop()函数。
        if(A.pop().equals(B.peek()))
            B.pop();   
    }   
    public int top() {
        return A.peek();
    }   
    public int min() {
        return B.peek();
    }
}
/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */
class MinStack:
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.A=[]
        self.B=[]
    def push(self, x: int) -> None:
        self.A.append(x)
        if not self.B or self.B[-1]>=x: self.B.append(x) 
    def pop(self) -> None:
        if self.A.pop()==self.B[-1]:
            self.B.pop()
    def top(self) -> int:
        return self.A[-1]
    def min(self) -> int:
        return self.B[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.min()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值