• 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;
    };
    
    
  • 相关阅读:
    避免Eclipse经常出现Out Of Memory
    java 判断类和实例的关系(instanceof,isInstance,isAssignableFrom)
    Tuscany SCA Core实现的SPI机制
    ubuntu下压缩和解压缩的命令用法
    eclipse 中引用其他项目及项目打包
    Tuscany 源码学习(1)
    Eclipse快捷键大全(转载)
    zz linux下用 SCP 命令进行网络传输
    HZNUACM寒假集训Day5小结 线段树 树状数组
    HZNUACM寒假集训Day1小结 STL 并查集
  • 原文地址:https://www.cnblogs.com/gongyanzh/p/12457092.html
Copyright © 2020-2023  润新知