03 2019 档案
摘要:题目描述; 第一次提交; class Solution: def isUnivalTree(self, root: TreeNode) -> bool: if root == None: return True if root.left!=None and root.left.val!=root.v
阅读全文
摘要:题目描述: 方法一: class Solution: def findMode(self, root: TreeNode) -> List[int]: if not root: return [] dic = {} stack = [root] while stack: node = stack.p
阅读全文
摘要:题目描述: 方法一:栈 class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: int """ count = 0 if root == None: r
阅读全文
摘要:题目描述: 方法一:递归 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: return 0 if root.left and root.left.left == None and root.
阅读全文
摘要:题目描述: 利用二叉搜索树的特点,如果p、q的值都小于root,说明p q 肯定在root的左子树中;如果p q都大于root,说明肯定在root的右子树中,如果一个在左一个在右 则说明此时的root记为对应的最近公共祖先 方法一:递归 class Solution: def lowestCommo
阅读全文
摘要:题目描述: 第一次提交:参考113-路径总和② class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: r = [] if not root: return r l = "" def path(root, l):
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return None temp = root.le
阅读全文
摘要:题目描述: 参考后的提交: class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ r = [] l = []
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if not root : return Fa
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 if root.left and root.ri
阅读全文
摘要:恢复内容开始 题目描述: 方法一: class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True if abs(self.c
阅读全文
摘要:恢复内容开始 题目描述: 方法一: 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
阅读全文
摘要:https://mp.weixin.qq.com/s/jluii9YIvfhKd_tPecfTaw
阅读全文
摘要:题目描述: 第一次提交: 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(
阅读全文
摘要:题目描述: 第一次提交: class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-
阅读全文
摘要:题目描述: 第一次提交: class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: if head==None or head.next==None: return head head.next = self.de
阅读全文
摘要:题目描述: 第一次提交:(超时) class Solution: def climbStairs(self, n: int) -> int: if n == 0 or n == 1 or n == 2: return n return self.climbStairs(n - 1) + self.c
阅读全文
摘要:题目描述: 第一次提交:(会超时) class Solution: def mySqrt(self, x: int) -> int: if x==0 or x==1: return x for i in range(1,x): if i*i<=x and (i+1)**2>x: return i V
阅读全文
摘要:题目描述: 第一次提交: class Solution: def addBinary(self, a: str, b: str) -> str: list_a,list_b=[],[] for s in a: list_a.append(int(s)) for s in b: list_b.appe
阅读全文
摘要:题目描述: 第一次提交: class Solution: def plusOne(self, digits): digits[-1]=digits[-1]+1 for i in range(len(digits)-1,0,-1): if digits[i]==10: digits[i]=0 digi
阅读全文
摘要:题目描述: 第一次解答: class Solution: def lengthOfLastWord(self, s: str) -> int: L=s.strip().split(" ") if L[-1]=="" : return 0 return len(L[-1]) 优化后: class So
阅读全文
摘要:题目描述: 方法一:O(N) class Solution(object): def maxSubArray(self, nums): sum = 0 max_sub_sum = nums[0] for num in nums: sum += num if sum > max_sub_sum: ma
阅读全文
摘要:恢复内容开始 题目描述: 第一次提交: class Solution: def countAndSay(self, n: int) -> str: f = "1" for i in range(n-1): count = 0 c = '' for j in range(len(f)): if j =
阅读全文
摘要:题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in range(len(nums)): if nums[i] >= target : return i if
阅读全文
摘要:题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle): return 0 for i in range(len(haystack)): if i+len(n
阅读全文
摘要:题目: 第一次提交: class Solution: def removeElement(self, nums, val: int) -> int: for i in range(len(nums)-1,-1, -1):#此处中间为range(,中间值为-1,) if nums[i] == val:
阅读全文
摘要:题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums) -> int: for i in range(len(nums)-1,0,-1):#注意要倒序** if nums[i]==nums[i-1]: del(nums[i]) re
阅读全文
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def merge
阅读全文
摘要:python字符串与列表的相互转换 学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'world'] 2. 列表转字符串 l = ["hi","hel
阅读全文
摘要:笔记: python if not 判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。 使用if not x这种写法的前提是:必须清楚x等于None, Fa
阅读全文
摘要:错误记录 class Solution: def romanToInt(self, s: str) -> int: d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} r=0 for i in range(len(s)): if d[s[
阅读全文
摘要:题目描述 方法一:转换为字符串 class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False else: y=str(x)[::-1] return y==str(x) 方法二;数字反转 class Solu
阅读全文
摘要:Python join()方法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的元素序列。 返回值 返回通过指定字符连接序列中元素后生成的新字符串。 实例 #!/usr/bin/python # -*- coding: UTF-8 -*- str
阅读全文
浙公网安备 33010602011771号