2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

1617-卿同学

发表文章数:84

热门标签

,
首页 » MySQL » 正文

523. 连续子序列的和是某数的倍数

class Solution:
    def checkSubarraySum(self, nums: List[int], k: int) -> bool:
        n = len(nums)
        if n<2:
            return False
        prefixsum = [0]
        for i in range(n):
            prefixsum.append(prefixsum[-1]+nums[i])
        prefixset =set()
        for i in range(2,n+1):
            prefixset.add(prefixsum[i-2]%k)
            if prefixsum[i] % k in prefixset:
                return True
        return False

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

变成一个图,之后用bfs去遍历连接的东西,然后再存起来 

class Solution:
    def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
        emailtoname = {}
        graph = defaultdict(set)
        for account in accounts:
            name = account[0]
            for email in account[1:]:
                graph[account[1]].add(email)
                graph[email].add(account[1])
                emailtoname[email] = name
        
        seen = set()
        ans = []
        for email in graph:
            if email not in seen:
                seen.add(email)
                stack = [email]
                component = []
                while stack:
                    node = stack.pop()
                    component.append(node)
                    for nei in graph[node]:
                        if nei not in seen:
                            seen.add(nei)
                            stack.append(nei)
                ans.append([emailtoname[email]] + sorted(component))
        return ans
        
        

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

class Solution:
    def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
        time = [0]*(n)
        stack=[]
        for i in logs:    
            id,pos,curtime = i.split(':')  
            id= int(id)
            curtime= int(curtime)
            if pos == 'start':    
                stack.append([id,curtime])
            else:
                prid,prtime = stack.pop()
                time[id] += curtime - prtime +1
                if stack:
                    time[stack[-1][0]] -= curtime - prtime +1
            
        return time
                

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

 2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

 类似于中序遍历: 左中右   inorder

(复习: 前序遍历:中左右  preorder   后序遍历:右中左 post order)

"""
# Definition for a Node.
class Node:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
"""

class Solution:
    def treeToDoublyList(self, root: 'Node') -> 'Node':
        if not root :
            return 
        self.first = None
        self.last = None
        self.dfs(root)
        self.first.left = self.last
        self.last.right = self.first
        return self.first
        
    def dfs(self,node):
        
        if not node:
            return
        self.dfs(node.left)
        if self.last:
            self.last.right = node
            node.left = self.last
        else:
            self.first = node
        self.last = node
        self.dfs(node.right)
        

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

先中序遍历变成list,之后再判断

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class BSTIterator:

    def __init__(self, root: Optional[TreeNode]):
        self.order = []
        self.dfsinorder(root)
        self.position = -1
        self.length = len(self.order)
    def next(self) -> int:
        self.position += 1
        return self.order[self.position]

    def hasNext(self) -> bool:
        if self.position +1 < self.length:
            return True
        else:
            return False
        
    def dfsinorder(self,node):
        if not node:
            return
        self.dfsinorder(node.left)
        self.order.append(node.val)
        self.dfsinorder(node.right)
        
        


# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

 2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

二分法: 如果是左右闭区间,则left<= right

# """
# This is BinaryMatrix's API interface.
# You should not implement it, or speculate about its implementation
# """
#class BinaryMatrix(object):
#    def get(self, row: int, col: int) -> int:
#    def dimensions(self) -> list[]:

class Solution:
    def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
        rows,cols = binaryMatrix.dimensions()
        mincol = [101]*rows
        for i in range(rows):
            x,y = 0,cols-1
            while x<=y:
                mid = (x+y) // 2
                condition = binaryMatrix.get(i,mid) == 1
                if mid == 0 and condition:
                    mincol[i] = mid
                    break
                elif condition and binaryMatrix.get(i,mid-1) == 0:
                    mincol[i] = mid
                    break
                elif condition:
                    y = mid - 1
                else:
                    x = mid + 1
        return min(mincol) if min(mincol) != 101 else -1 
                    

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

class Solution:
    def findBuildings(self, heights: List[int]) -> List[int]:
        n = len(heights)
        maxvalue = 0
        ans = []
        for i in range(n-1,-1,-1):
            if heights[i] > maxvalue:
                ans.append(i)
                maxvalue = heights[i]
        return ans[::-1]

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        hour = hour % 12
        degreehour = 360*(hour / 12) + (minutes/60) *30
        degreemin = 360*(minutes / 60)
        diff1 = abs(degreemin-degreehour)
        diff2 = 360 - diff1
        return min(diff1,diff2)

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
        ans = 0
        if not root:
            return 0
        def dfs(node):
            nonlocal ans
            if not node:
                return
            if node.val >= low and node.val<= high:
                ans += node.val
            dfs(node.left)
            dfs(node.right)
        dfs(root)
        return ans
        

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

2021-09-23:连续子序列的和是某数的倍数,accounts merge邻接表graph, stack cpu function, 二叉树双向链表,二叉树迭代器,二分法最左为1,二叉树列排序

notice:

sort 列表的时候会按照[0], [1] 的方式来sort,所以用一个lambda只排序第一个

其次,越接近root的点要越前面

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root:
            return []
        dictnode = defaultdict(list)
        start = 0
        deep = 0
        def addnode(node,position,deep):
            if not node:
                return
            nonlocal dictnode
            dictnode[position] += [[deep,node.val]]
            addnode(node.left,position-1,deep+1)
            addnode(node.right,position+1,deep+1)
        addnode(root,start,deep)
        minvalue = min(dictnode)
        maxvalue = max(dictnode)
        ans =[]
        print(dictnode)
        for i in range(minvalue,maxvalue+1):
            if dictnode[i]:
                dictnode[i].sort(key= lambda a:a[0])
                ans += [[node[1] for node in  dictnode[i]]]
        return ans
                
标签:
分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录