leetcood学习笔记-9】的更多相关文章

python字符串与列表的相互转换   学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'world'] 2. 列表转字符串 l = ["hi","hello","world"] print(" ".join(l))输出:hi hello…
笔记: 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…
题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: res = ListNode(None) node = res while l1 and…
错误记录 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 isPalindrome(self, x: int) -> bool: if x<0: return False else: y=str(x)[::-1] return y==str(x) 方法二;数字反转 class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False a,res=x,0 while x: x,mod =…
Python join()方法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的元素序列. 返回值 返回通过指定字符连接序列中元素后生成的新字符串. 实例 #!/usr/bin/python # -*- coding: UTF-8 -*- str = "-"; seq = ("a", "b", "c"); # 字符串序列 print str.join( seq ); 以上实例…
题目描述: 方法一:asiic码 class Solution: def convertToTitle(self, n: int) -> str: if (n-1)//26 == 0: return chr(65+(n-1) % 26)#参数是0 - 256 的一个整数,返回值是当前整数对应的ascii字符.参数可以是10进制也可以是16进制的形式 else: return self.convertToTitle((n-1)//26) + chr(65 +(n-1)%26)…
题目描述: 第一次提交: 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 deleteDuplicates(self, head: ListNode) -> ListNode: if head==None or head.next==None: return head head.next = self.deleteDuplicates(head.next) if head.val==head.next.val: head=head.next # python无需像C++一样手动释放内存 return h…
题目描述: 第一次提交:(超时) 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 lengthOfLastWord(self, s: str) -> int: L=s.strip().split(" ") if L[-1]=="" : return 0 return len(L[-1]) 优化后: class Solution: def lengthOfLastWord(self, s: str) -> int: return len(s.strip().split('…
题目描述; 第一次提交; 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(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: int """ count = 0 if root == None: return count stack = [(root,[root.val])] while stack != []: tree,number = st…
题目描述: 方法一:递归 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…
题目描述: 利用二叉搜索树的特点,如果p.q的值都小于root,说明p q 肯定在root的左子树中:如果p q都大于root,说明肯定在root的右子树中,如果一个在左一个在右 则说明此时的root记为对应的最近公共祖先 方法一:递归 class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if max(p.val,q.val) <…
题目描述: 第一次提交:参考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 invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return None temp = root.left root.left = root.right root.right = temp# root.left,root.right = root.ri…
题目描述: 参考后的提交: 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…