随笔分类 - leetcode
摘要:恢复内容开始 题目描述: 方法一: class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if not nums: return None el
阅读全文
摘要:题目描述: 方法一: class Solution: def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """
阅读全文
摘要:题目描述: 方法一; class Solution(object): def levelOrder(self, root): if not root: return [] ans = [] stack = [root] while stack: tem_stack = [] tem_ans = []
阅读全文
摘要:题目描述: 方法一: class Solution(object): def levelOrderBottom(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ if not root: return [] ans =
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 elif root.left==None and
阅读全文
摘要:题目描述: 方法一:递归: class Solution: def isSymmetric(self, root: TreeNode) -> bool: if not root: return True def Tree(p, q): if not p and not q: return True
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """ a = 0 b =
阅读全文
摘要:题目描述: 方法一:排序输出中位数 class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return None nums.sor
阅读全文
摘要:题目描述: 解法一:迭代 class Solution: def reverseList(self, head: ListNode) -> ListNode: pre = None cur = head while cur: tmp = cur.next cur.next = pre cur,pre
阅读全文
摘要:题目描述: 方法:#在改pre链表时 head中的值也改变 class Solution(object): def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode ""
阅读全文
摘要:题目描述: 方法一: class Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ a,b = headA,head
阅读全文
摘要:题目描述: 方法一: class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ while head: if head.val == "daafas": return Tru
阅读全文
摘要:题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0,1 l = [] if matrix==[]: return [] m = len(matrix) n
阅读全文
摘要:题目描述: 参考后的提交: class Solution: def generateMatrix(self, n: int): #l = [[0] * n] * n 此创建方法错误 l = [[] for i in range(n)] for i in range(n): for j in rang
阅读全文
摘要:题目描述: 第一次提交;(超时): class Solution: def countPrimes(self, n: int) -> int: count = 0 for i in range(2,n): for j in range(2,i+1): if i%j == 0 and j!=i: br
阅读全文
摘要:题目描述: 方法一:比较笨的办法,根据题意,如果变成1返回True,如果出现重复返回False 看到下面有位朋友用的是dict,我用了list,两个都跑了一下似乎list快一点? class Solution: def isHappy(self, n: int) -> bool: l = [] wh
阅读全文
摘要:题目描述: 方法:不断除以 5, 是因为每间隔 5 个数有一个数可以被 5 整除, 然后在这些可被 5 整除的数中, 每间隔 5 个数又有一个可以被 25 整除, 故要再除一次, ... 直到结果为 0, 表示没有能继续被 5 整除的数了 class Solution: def trailingZe
阅读全文
摘要:题目描述: 方法: class Solution: def titleToNumber(self, s: str) -> int: num = 0 r = 1 for i in s[::-1]: num +=(ord(i)-64)*r r*=26 return num
阅读全文
摘要:题目描述: 方法一:asiic码 class Solution: def convertToTitle(self, n: int) -> str: if (n-1)//26 == 0: return chr(65+(n-1) % 26)#参数是0 - 256 的一个整数,返回值是当前整数对应的asc
阅读全文
摘要:题目描述: 第一次提交: class Solution: def generate(self, numRows: int): l = [] for i in range(numRows): n = [1]*(i+1) if len(n)>2: #pre = [1,1] for j in range(
阅读全文
浙公网安备 33010602011771号