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;
}
};
- 用队列实现栈
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
- 以大数据之名,变身!——In big data we trust
- 90%家长都不知道关于少儿编程的这些疑题!
- 常用SQL语句和语法汇总
- Python学习笔记1——斐波那契数列
- 视觉传感器几大技术要点详解!
- Spark之搜狗日志查询实战
- 区块链与数字货币是什么关系呢?
- 保存数据到MySql数据库——我用scrapy写爬虫(二)
- 人工智能将取代人类?危机亦或是新的机遇
- 大数据驱动的未来网络:体系架构与应用场景(下)网络架构与场景详解
- 冷静点,NVIDIA 禁止 Geforce 进数据中心想限制的并不是深度学习
- 智能机器人崛起背后的中国力量
- 企业微服务架构转型-实施步骤
- Andrew Ng机器学习课程笔记--week2(多元线性回归&正规公式)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 案例实战 | Python 玩转 AB 测试中的分层抽样与假设检验!(附代码和数据集)
- Python 3.10 明年发布,看看都有哪些新特性?
- R可视化 | 混合多个图形
- 爱了!爱了!一款用 pandas 玩转 SQL 的神器
- 详细了解JS Map,它和传统对象有什么区别?
- Java HelloWorld 學習
- Bytecodes.java
- GenerateOopMap.java
- CellTypeState.java
- 终于开始了,微软的野心将通过全场景开发平台.NET 5体现得淋漓尽致!
- 微信小程序开发实战(27):录音
- 微信小程序开发实战(28):播放、暂停、停止声音
- 微信小程序开发实战(29):控制背景音乐
- 面试:如何从 100 亿 URL 中找出相同的 URL?
- Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架