头像

隔岸烟火




离线:2天前


最近来访(2)
用户头像
万俟清泫
用户头像


a = int(input())
b = int(input())
n = int(input())

minn = min(a, b)
maxn = max(a, b)
r = min(minn, n)
l = max(n - maxn, 0)
print(r - l + 1)


活动打卡代码 AcWing 86. 构建乘积数组

隔岸烟火
10个月前
class Solution(object):
    def multiply(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        n = len(A)
        B = [1]*n
        p = 1
        for i in range(n):
            B[i] = p
            p *= A[i]
        p = 1
        for i in range(n)[::-1]:
            B[i] *= p
            p *= A[i]
        return B



隔岸烟火
10个月前
class Solution(object):
    def add(self, num1, num2):
        """
        :type num1: int
        :type num2: int
        :rtype: int
        """
        while num2:
            sum = num1 ^ num2
            carry = (num1 & num2) << 1
            num1 = sum
            num2 = carry
        return num1


活动打卡代码 AcWing 84. 求1+2+…+n

隔岸烟火
10个月前
class Solution(object):
    def getSum(self, n):
        """
        :type n: int
        :rtype: int
        """
        res = n
        n > 0  & res += self.getSum(n - 1)
        return res



隔岸烟火
10个月前
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        递归函数返回的是最低公共祖先
        如果root == p or root == q,有两种情况,
            一是p 和 q 都在root下面,二是只有一个,
            不管如何,都只需要返回root即可。
        如果 root 和p, q都不等,则
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        # 停止条件
        if not root:
            return False
        if root == p or root == q:
            return root
        # 操作

        # 递归式
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        # 返回方式
        if left and right:
            return root
        if left:
            return left
        if right:
            return right
        return False



隔岸烟火
10个月前
class Solution(object):
    def strToInt(self, str):
        """
        :type str: str
        :rtype: int
        """
        if not len(str):
            return 0
        temp = []
        i = 0
        minus_flag = False
        while str[i] == " ":
            i += 1
        if str[i] in "+-":
            if str[i] == "-":
                minus_flag = True
            i += 1
        while i < len(str) and "0" <= str[i] <= "9":
            temp.append(int(str[i]) - int('0'))
            i += 1
        res = 0
        for x in temp:
            res = res*10 + x

        if res >= 2147483647:
            res = 2147483647
            if minus_flag:
                res += 1
        return - res if minus_flag else res



隔岸烟火
10个月前
class Solution(object):
    def maxDiff(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        min = nums[0]
        res = 0
        for i in range(1, len(nums)):
            if nums[i] < min:
                min = nums[i]
            else:
                temp = nums[i] - min
                if temp > res:
                    res = temp
        return res



隔岸烟火
10个月前
class Solution(object):
    def lastRemaining(self, n, m):
        """
        :type n: int
        :type m: int
        :rtype: int
        """
        f = 0
        for i in range(2, n + 1):
            f = (f + m)%i
        return f




隔岸烟火
10个月前
class Solution(object):
    def maxInWindows(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        res = []
        q = []         # 单调队列,里面存下标
        for i in range(len(nums)):
            if q and q[0] <= i - k:   # 删除滑动窗口前面出去的元素
                q.pop(0)
            while q and nums[q[-1]] < nums[i]:        # 删除单调队列的队尾里永远不会用到的数字
                q.pop()
            q.append(i)
            if i >= k - 1:               # 输出
                res.append(nums[q[0]])
        return res



隔岸烟火
10个月前

没有用额外的空间去维护单调队列,不过时间复杂度为o(nk +k ^2)

class Solution(object):
    def maxInWindows(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        res = []
        if not nums:
            return res
        max = 0
        for i in range(1, k):
            if nums[i] > nums[max]:
                max = i
        res.append(nums[max])
        for i in range(k, len(nums)):
            if nums[i] > nums[max]:
                max = i
            elif max <= i - k:
                max = i - k + 1
                for j in range(i - k + 1, i + 1):
                    if nums[j] > nums[max]:
                        max = j
            res.append(nums[max])
        return res