笔记: 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, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行 链接:https://www.cnblogs.com/chenya/p/4218761.…
题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0,1 l = [] if matrix==[]: return [] m = len(matrix) n = len(matrix[0]) while x<=n*m: for i in range(j,n-j): l.append(matrix[j][i]) x += 1 for i in range(…
题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in range(len(nums)): if nums[i] >= target : return i if i == (len(nums)-1) and nums[i]<target: return i+1 法二: class Solution: def searchInsert(self, num…
题目: 第一次提交: 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(needle)<=len(haystack): if haystack[i:(i+len(needle))]==needle: return i return -1 方法二: Sunday 平均O(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: nums.remove(nums[i])#或nums.pop(i) return len(nums) 方法二:正序 class Solution: def removeElement(self, n…
题目描述: 第一次提交: 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]) return len(nums) 另: class Solution: def removeDuplicates(self, nums: List[int]) -> int: i = 0 for num…
错误记录 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[i]]<d[s[i+1]] and i<len(s)-1: r-=d[s(i)] else: r+=d[s(i)] return r 会报错:TypeError: 'str' object i…
题目描述: 第一次提交: 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(1,len(n)-1): n[j] = pre[j-1]+pre[j] #l.append([n]) l+=[n] pre = n return l 方法二: def generate(self, n…
题目描述: 第一次提交: class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ for i in range(n): nums1.remove(nums1[-1]) for i in r…
题目描述: 第一次提交:(超时) 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.climbStairs(n - 2) 方法一:带记忆递归 class Solution(object): def __init__(self): self.cache = [1,2] def climb…
题目描述: 第一次提交:(会超时) 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 方法二:牛顿迭代法(最优解)(泰勒展开式) class Solution: def mySqrt(self, x): """ :type x: int :rtype…
题目描述: 第一次提交: 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.append(int(s)) if len(list_a)>=len(list_b): for i in range(len(list_a)-len(list_b)): list_b.insert(0,0)…
题目描述: 第一次提交: 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 digits[i-1]=digits[i-1]+1 if digits[0]==10: digits[0]=0 digits.insert(0,1) return digits 法二: class Solut…
题目描述; 第一次提交; class Solution: def isUnivalTree(self, root: TreeNode) -> bool: if root == None: return True if root.left!=None and root.left.val!=root.val: return False if root.right!=None and root.right.val != root.val: return False if self.isUnivalTr…
题目描述: 方法一: class Solution: def findMode(self, root: TreeNode) -> List[int]: if not root: return [] dic = {} stack = [root] while stack: node = stack.pop() if node.val not in dic: dic[node.val] = 0 dic[node.val] += 1 if node.left: stack.append(node.le…
题目描述: 方法一:递归 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: return 0 if root.left and root.left.left == None and root.left.right == None: return root.left.val+self.sumOfLeftLeaves(root.right) else: return self.sumOfLef…
题目描述: 第一次提交:参考113-路径总和② class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: r = [] if not root: return r l = "" def path(root, l): if not root: return l += str(root.val) if not root.left and not root.right: r.append(l) l +…
题目描述: 参考后的提交: class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ r = [] l = [] if not root: return r def path(root, l , sum): if not root: return l.append…
题目描述: 第一次提交: class Solution(object): def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if not root : return False if sum - root.val == 0 and not root.left and not root.right: return Tru…
题目描述: 第一次提交: class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 if root.left and root.right: return min(self.minDepth(root.left)+1,self.minDepth(root.right)+1) if…