• 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;
        }
    };
    

    255.用队列实现栈

    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;
    };
    
    
  • 相关阅读:
    #import &lt;/usr/include/objc/objc-class.h&gt; not such file or directory问题的解决方法
    关于二进制补码
    DirectSound的应用
    Qt on Android: http下载与Json解析
    双绞线的制作,T568A线序,T568B线序
    Java设计模式之从[暗黑破坏神存档点]分析备忘录(Memento)模式
    linux 系统升级中的一些配置
    malloc函数具体解释
    老鸟的Python新手教程
    ubuntu12.04 安装配置jdk1.7
  • 原文地址:https://www.cnblogs.com/gongyanzh/p/12457092.html
Copyright © 2020-2023  润新知