跳至正文
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
小奥的学习笔记

万丈高楼平地起,勿在浮沙筑高台

小奥的学习笔记

万丈高楼平地起,勿在浮沙筑高台

  • 首页
  • Sample Page
  • 隐私策略
  • GuestBook
  • 慕课课程(MOOC’s Lessons)
  • Footprints
  • About Me
  • 个人履历
  • 首页
  • Sample Page
  • 隐私策略
  • GuestBook
  • 慕课课程(MOOC’s Lessons)
  • Footprints
  • About Me
  • 个人履历
关

搜索

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Leetcode

Leetcode题目解析(191126):85&94

作者 yszhang
2019年11月26日 1 分钟阅读
0

Leetcode 85:最大矩形

题目描述

给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

代码实现

int maximalHistRec(vector<int>& height) {
    stack<int> stackStore;
    int res = height[0];
    for (int i = 0; i < height.size(); i++) {
        if (stackStore.empty() || height[i] >= height[stackStore.top()])
            stackStore.push(i);
        else {
            while (!stackStore.empty() && height[stackStore.top()] > height[i]) {
                int ind = stackStore.top();
                stackStore.pop();
                res = max(res, stackStore.empty() ? height[ind] * i : height[ind] * (i - stackStore.top() - 1));
            }
            stackStore.push(i);
        }
    }
    return res;
}
int maximalRectangle(vector<vector<char> >& matrix) {
    if (matrix.size() == 0)
        return 0;
    int res = 0;
    vector<int> height;
    for (int i = 0; i < matrix[0].size(); i++) {
        if (matrix[0][i] == '1')
            height.push_back(1);
        else
            height.push_back(0);
    }
    height.push_back(0);
    res = maximalHistRec(height);
    for (int i = 1; i < matrix.size(); i++) {
        for (int j = 0; j < matrix[0].size(); j++) {
            if (matrix[i][j] == '1')
                height[j] += 1;
            else
                height[j] = 0;
        }
        res = max(res, maximalHistRec(height));
    }
    return res;
}

参考自:
https://www.nowcoder.com/profile/1941588/codeBookDetail?submissionId=12684718

执行用时&内存消耗

复杂度分析

Leetcode 94:二叉树的中序遍历

题目描述

给定一个二叉树,返回它的中序遍历。请使用迭代算法

代码实现

vector<int> inorderTraversal(TreeNode* root)
{
    stack<TreeNode* > stack;
    vector<int> res;
    TreeNode* tmp=root;
    while(tmp||stack.size())
    {
        while(tmp){
            stack.push(tmp);
            tmp=tmp->left;
        }
        tmp=stack.top();
        stack.pop();
        res.push_back(tmp->val);
        tmp=tmp->right;
    }
    return res;
}

时间和空间占用

复杂度分析

作者

yszhang

关注我
其他文章
上一个

Leetcode题目解析(191125):96&98

下一个

已修:计算机组成原理(上)【哈尔滨工业大学】[2019-01-25]

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Copyright 2026 — 小奥的学习笔记. All rights reserved. Blogsy WordPress Theme