LeetCode-0310

时间:2020-03-10
本文章向大家介绍LeetCode-0310,主要包括LeetCode-0310使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

121.买卖股票的最佳时机

python

# 记录下最低交易价格,同时记录目前为止最高收益
def MaxProfit(prices):
    maxprofit,minprice = 0,prices[0]
    for price in prices:
        maxprofit = max(price-minprice,maxprofit)
        minprice = min(price,minprice)
    return maxprofit

c++

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(!prices.empty()){
            int maxprofit = 0;
            int minprices = prices[0];
            for(auto price:prices){
                maxprofit = max(price-minprices,maxprofit);
                minprices = min(price,minprices);
            }
            return maxprofit;
        }
        else{
            return 0;
        }
    }
};

543.二叉树的直径

python

#深度优先遍历,递归求节点深度, 找到最大深度,减一即为长度
class Solution:
    def diameterOfBinaryTree(self, root: TreeNode) -> int:
        self.ans = 0
        def depth(node):
            if not node:
                return 0
            left = depth(node.left)#递归左儿子深度
            right = depth(node.right)
            self.ans = max(left+right+1,self.ans)#保存最大深度
            return max(left,right)+1
        depth(root)
        return self.ans-1

c++

/**
 * 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 {
    int ans;    //声明全局变量
    int depth(TreeNode* node){
        if(node == NULL){
            return 0;
        }
        int L = depth(node->left);
        int R = depth(node->right);
        ans = max(L+R+1,ans);
        return max(L,R)+1;
    }
public:
    int diameterOfBinaryTree(TreeNode* root) {
        if(root==NULL){
            return 0;
        }
        ans = 0;
        depth(root);
        return ans-1;
    }
};
  1. 用队列实现栈

python

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.stack = []

    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.stack.append(x)

    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        return self.stack.pop(-1)

    def top(self) -> int:
        """
        Get the top element.
        """
        return self.stack[-1]

    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        if self.stack:
            return False
        else:
            return True

c++

# 单队列实现
class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        que.push(x);
        for(int i=0;i+1<que.size();i++){    //push 后队列size已经改变
            int temp = que.front();
            que.pop();
            que.push(temp);
            
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int val = top();
        que.pop();
        return val;
    }
    
    /** Get the top element. */
    int top() {
        return que.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return que.empty();
    }

private:
    queue<int> que;
};

原文地址:https://www.cnblogs.com/gongyanzh/p/12457092.html