文章分类 -  Leetcode

摘要:思路:并查集+因数分解 将nums中的每个数c进行因数分解,而后将c和该因子合并(merge)到并查集中 对nums中的每个数找到其祖先,并将祖先的子节点(cnt数组)加1并选择拥有最多子节点的祖先节点的子节点数 class Solution { public void merge(int[]fa, 阅读全文
posted @ 2022-07-30 10:54 kris-phl 阅读(2) 评论(0) 推荐(0) 编辑
摘要:class Solution: def fractionAddition(self, exp: str) -> str: cnt=[] i,n,m,s=0,len(exp),1,0 while i<n: if exp[i]>='0' and exp[i]<='9': c=0 while i<n an 阅读全文
posted @ 2022-07-27 10:06 kris-phl 阅读(2) 评论(0) 推荐(0) 编辑
摘要:public class Node{ public Node[]next;//节点在不同层的下一节点,最下面为第一层,包含所有节点值 public int val; public Node(int val,int size){ this.val=val; this.next=new Node[siz 阅读全文
posted @ 2022-07-26 13:54 kris-phl 阅读(3) 评论(0) 推荐(0) 编辑
摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self. 阅读全文
posted @ 2022-07-25 09:53 kris-phl 阅读(3) 评论(0) 推荐(0) 编辑
摘要:class Solution: def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int: l1=0 for i in range(min(start,destination 阅读全文
posted @ 2022-07-24 09:25 kris-phl 阅读(2) 评论(0) 推荐(0) 编辑
摘要:思路1:排序+贪心 先将nums按nums[i][1]进行升序排序,而后贪心地在每个区间中选取两个数。初始时,选取第一个区间的最右边两个数,即l=nums[0][1]-1和r=nums[0][1],因此res初始值设为2,而后遍历其他区间时,设当前遍历到的区间为[a,b] 若l和r均在该区间内,即 阅读全文
posted @ 2022-07-22 11:15 kris-phl 阅读(10) 评论(0) 推荐(0) 编辑
摘要:class Solution: def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def check(root): if root==None: return False l,r=check(root.left) 阅读全文
posted @ 2022-07-21 09:39 kris-phl 阅读(5) 评论(0) 推荐(0) 编辑
摘要:class Solution: def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: m,n=len(grid),len(grid[0]) k%=n*m res=[[0]*n for i in range(0,m 阅读全文
posted @ 2022-07-20 09:27 kris-phl 阅读(2) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示