"""
# Definition for a Node.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.parent = None
"""
class Solution:
def lowestCommonAncestor(self, p: 'Node', q: 'Node') -> 'Node':
a = p
b = q
while (p != q):
p = b if not p else p.parent
q = a if not q else q.parent
return p
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
flag = 0
for i in range(n-2,-1,-1):
if nums[i] < nums[i+1]:
flag = 1
for j in range(n-1,i,-1):
if nums[j] > nums[i]:
nums[i],nums[j] = nums[j], nums[i]
for k in range((n-1-i)//2):
nums[i+1+k], nums[n-1-k] = nums[n-1-k], nums[i+1+k]
break
break
if flag == 0:
nums.reverse()
return
求向量和:
class SparseVector:
def __init__(self, nums: List[int]):
self.nums = nums
# Return the dotProduct of two sparse vectors
def dotProduct(self, vec: 'SparseVector') -> int:
ans = 0
for i in range(len(self.nums)):
ans += self.nums[i]*vec.nums[i]
return ans
# Your SparseVector object will be instantiated and called as such:
# v1 = SparseVector(nums1)
# v2 = SparseVector(nums2)
# ans = v1.dotProduct(v2)