2021-09-20:facebook 刷题 最近父节点链表环,字典序大小,向量和

"""
# 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值