摘要:前中后序三种遍历方法对于左右结点的遍历顺序都是一样的(先左后右),唯一不同的就是根节点的出现位置。 1.前序:前序遍历指根结点在最前面输出,所以前序遍历的顺序是:中左右。 递归:class Solution: def preorderTraversal(self, root): ##前序遍历 """
阅读全文
摘要:法一: class Solution: def hasCycle(self, head: ListNode) -> bool: a = set() while head: if head in a: return True a.add(head) head = head.next return Fa
阅读全文
摘要:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None 法一: class Solution: def mergeTwoList
阅读全文
摘要:法一:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeNthFrom
阅读全文
摘要:class Solution: def convert(self, s: str, numRows: int) -> str: if numRows < 2: return s res = ["" for _ in range(numRows)] i, flag = 0, -1 for c in s
阅读全文
摘要:class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() if not nums or len(nums)<3: return
阅读全文
摘要:法一: class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: ret = [] for i in range(len(nums)): for j in range(1,len(nums)-i): if
阅读全文
摘要:法一: class Solution: def plusOne(self, digits: List[int]) -> List[int]: length = len(digits) res = 0 for i in digits: if digits[-1]!=9: if length!=1: r
阅读全文
摘要:法一: class Solution: def removeElement(self, nums: List[int], val: int) -> int: for i in range(len(nums)-1, -1, -1): if(nums[i] == val): nums.pop(i) re
阅读全文
摘要:法一: class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ n = len(nums) k %
阅读全文
摘要:法一:class Solution: def maxProfit(self, prices: List[int]) -> int: ret = 0 for i in range(len(prices)): if i+1<len(prices): if prices[i]<prices[i+1]: r
阅读全文
摘要:法一:class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not strs: return '' str_min = min(strs) str_max = max(strs) for i in rang
阅读全文
摘要:法一:def intersect(nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ ans = [] nums1.sort() nums2.sort() i = j = 0 w
阅读全文
摘要:编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。 + + +| Id | Salary |+ + +| 1 | 100 || 2 | 200 || 3 | 300 |+ + +例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在
阅读全文
摘要:表1: Person + + +| 列名 | 类型 |+ + +| PersonId | int || FirstName | varchar || LastName | varchar |+ + +PersonId 是上表主键表2: Address + + +| 列名 | 类型 |+ + +| A
阅读全文