LeetCode日记20200218

LeetCode日记2020.2.18


5数组 + 1随机 mid + 1随机 hard

723 我的日程安排表(hard)

自己的较慢速解法,以后要用set时问问自己:是否会有重复数据?

class MyCalendarThree {
public:
    MyCalendarThree() {
        
    }
    
    int book(int start, int end) {
        calen.insert(make_pair(start, end));
        int res = 0;
        priority_queue<int, vector<int>, greater<int>> pq;
        for(const auto& c: calen)
        {
            while(!pq.empty() && pq.top() <= c.first)
            {
                pq.pop();
            }
            pq.push(c.second);
            res = max(res, int(pq.size()));
        }
        return res;
    }

    multiset<pair<int, int>> calen;
};

/**
 * Your MyCalendarThree object will be instantiated and called as such:
 * MyCalendarThree* obj = new MyCalendarThree();
 * int param_1 = obj->book(start,end);
 */

优化后快速解法:

class MyCalendarThree {
public:
    MyCalendarThree() {
        
    }
    
    int book(int start, int end) {
        if(calen.find(start) != calen.end())
            ++calen[start];
        else
            calen[start] = 1;

        if(calen.find(end) != calen.end())
            --calen[end];
        else
            calen[end] = -1;

        int res = 0;
        int k = 0;
        for(const auto& c: calen)
        {
            k += c.second;
            if(k>res)
                res = k;
        }
        return res;
    }

    map<int, int> calen;
};

/**
 * Your MyCalendarThree object will be instantiated and called as such:
 * MyCalendarThree* obj = new MyCalendarThree();
 * int param_1 = obj->book(start,end);
 */

986 区间列表的交集(mid)

注意利用每个列表内区间排序且互不相交的性质。

class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
        vector<vector<int>> res;
        int i=0, j=0;
        while(i<A.size()&&j<B.size())
        {
            if(A[i][0]<=B[j][1] && A[i][1]>=B[j][0])
                res.push_back(vector<int>{max(A[i][0], B[j][0]), min(A[i][1], B[j][1])});

            if(A[i][1] == B[j][1])
            {
                ++i;
                ++j;
            }
            else if(A[i][1]<B[j][1])
                ++i;
            else
                ++j;
        }
        return res;
    }
};

442 数组中的重复数据(mid)

class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        vector<int> res;
        for(int i=0;i<nums.size();++i)
        {
            int num = abs(nums[i]);
            if(nums[num-1]<0)
                res.push_back(num);

            nums[num-1] = -nums[num-1];
        }
        return res;
    }
};

64 最小路径和(mid)

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid.front().size();
        vector<int> dp(n+1, 0);
        for(int i=n-1;i>=0;--i)
            dp[i] = dp[i+1] + grid[m-1][i];
        
        dp[n] = numeric_limits<int>::max();
        for(int i=m-2;i>=0;--i)
        {
            for(int j=n-1;j>=0;--j)
            {
                dp[j] = grid[i][j] + min(dp[j], dp[j+1]);
            }
        }
        return dp[0];
    }
};

238 除自身以外数组的乘积(mid)

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n, 1);
        int prod = 1;
        for(int i=0;i<n;++i)
        {
            res[i] *= prod;
            prod *= nums[i];
        }
        prod = 1;
        for(int i=n-1;i>=0;--i)
        {
            res[i] *= prod;
            prod *= nums[i];
        }
        return res;
    }
};

106 从中序与后序遍历序列构造二叉树(mid)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    using Iter = vector<int>::const_iterator;
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        reverse(postorder.begin(), postorder.end());
        return buildT(inorder.cbegin(), inorder.cend(), postorder.cbegin(), postorder.cend());
    }

    TreeNode* buildT(Iter inBeg, Iter inEnd, Iter pBeg, Iter pEnd)
    {
        if(pBeg == pEnd)
            return NULL;
        
        int val = *pBeg;
        TreeNode* root = new TreeNode(val);
        auto pos = find(inBeg, inEnd, val);
        int dis = inEnd - pos - 1;
        auto lpEnd = pBeg + 1;
        advance(lpEnd, dis);
        root->right = buildT(pos+1, inEnd, pBeg + 1, lpEnd);
        root->left = buildT(inBeg, pos, lpEnd, pEnd);
        return root;
    }
};

48 旋转图像(mid)

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n=matrix.size();
        for(int i=0;i<n;++i)
        {
            for(int j=i;j<n;++j)
            {
                std::swap(matrix[i][j], matrix[j][i]);
            }
        }
        for(int i=0;i<n;++i)
        {
            for(int j=0;j<n/2;++j)
            {
                std::swap(matrix[i][j], matrix[i][n-1-j]);
            }
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值