classSolution:deflargestRectangleArea(self,heights:List[int])->int:# 先在左右加两个值,防止一直单调递增无法出栈heights=[0]+heights+[0]# 单调栈stack=[]# 返回值ret=0# 遍历foriinrange(len(heights)):# 如果当前值大于栈顶,直接入栈# 反之,不断出栈,直到栈顶元素小于当前值whilestackandheights[stac...
#Definition for a binary tree node.#class TreeNode(object):#def __init__(self, x):#self.val = x#self.left = None#self.right = NoneclassSolution(object):defmaxDepth(self, root):""":type root: TreeNode :rtype: int"""returnself.search(root, 0)defsearch(self, node, i):ifnodeis...
这就需要同学具有抽象问题的能力:DP题目本手:子问题的数学关系+记忆矩阵class Solution { public ...
classSolution(object):deftitleToNumber(self, s):""":type s: str :rtype: int"""s=s.upper() list_n=list(s) output=0foreachinlist_n: output+= ord() - ord('A') + 1returnoutput Valid Anagram Core: Two pointer classSolution(object):defisAnagram(self, s, t):""":type s: str ...
对应的 Java 仓库的地址,传送门:https://github.com/liweiwei1419/LeetCode-Solution-Java 说明:现在刷题,尤其是写题解,绝大多数问题都会写两个语言的代码,Java 是我的母语,Python 是我的新欢。 发布在 LeetCode 中文版上的题解配图使用的 PPT,传送门:https://github.com/liweiwei1419/LeetCode-Solution-PPT...
from collectionsimportdefaultdictclassSolution:defmaxPoints(self,points):""":type points:List[Point]:rtype:int""" slopes,result=defaultdict(int),0fori,point1inenumerate(points):slopes.clear()duplicate=1for_,point2inenumerate(points[i+1:]):ifpoint1.x==point2.x and point1.y==point2.y:dup...
今天的题目来源于 LeetCode 中第 42 题:接雨水,hard 级别,目前通过率 50.8% 。 题目描述: 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例: 代码语言:javascript 代码运行次数:0 输入:[0,1,0,2,1,0,1,3,2,1,2,1]输出:6 ...
Tags: LeetCode, Python, Programming, Solution, Hard, Algorithm Slug: leetcode-765 Author: ouankou LeetCode 第772题 Basic Calculator III 简要分析及Python代码 题目: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open and closing parentheses , ...
有人做的比java慢,我猜原因可能是有些函数在python执行时候是以c的速度执行的(python学习手册上说的...
#TitleSolutionBasic idea (One line) 1 Two Sum Python Java 1. Hash O(n) and O(n) space.2. Sort and search with two points O(n) and O(1) space. 2 Add Two Numbers Python Java Take care of the carry from lower digit. 3 Longest Substring Without Repeating Characters Python Java 1....