滁州市南谯区建设局网站,任家房网站建设,昆明市住房和城乡建设局网站上看的,设计外贸网站建设目录
1. 合并两个有序数组
2. 二叉树的右视图
3. 拼接最大数
#x1f31f; 每日一练刷题专栏
C/C 每日一练 专栏
Python 每日一练 专栏 1. 合并两个有序数组
给你两个有序整数数组 nums1 和 nums2#xff0c;请你将 nums2 合并到 nums1 中#xff0c;使 nums1 成为…
目录
1. 合并两个有序数组
2. 二叉树的右视图
3. 拼接最大数 每日一练刷题专栏
C/C 每日一练 专栏
Python 每日一练 专栏 1. 合并两个有序数组
给你两个有序整数数组 nums1 和 nums2请你将 nums2 合并到 nums1 中使 nums1 成为一个有序数组。
初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。你可以假设 nums1 的空间大小等于 m n这样它就有足够的空间保存来自 nums2 的元素。
示例 1
输入nums1 [1,2,3,0,0,0], m 3, nums2 [2,5,6], n 3
输出[1,2,2,3,5,6]
示例 2
输入nums1 [1], m 1, nums2 [], n 0
输出[1]提示
nums1.length m nnums2.length n0 m, n 2001 m n 200-10^9 nums1[i], nums2[i] 10^9
代码
class Solution(object):def merge(self, nums1, m, nums2, n)::type nums1: List[int]:type m: int:type nums2: List[int]:type n: int:rtype: void Do not return anything, modify nums1 in-place instead.p1, p2 m - 1, n - 1pos m n - 1while p1 0 and p2 0:if nums1[p1] nums2[p2]:nums1[pos] nums1[p1]p1 - 1else:nums1[pos] nums2[p2]p2 - 1pos - 1while p2 0:nums1[pos] nums2[p2]p2 - 1pos - 1return nums1# %%
s Solution()
print(s.merge(nums1 [1,2,3,0,0,0], m 3, nums2 [2,5,6], n 3))输出
[1, 2, 2, 3, 5, 6] 2. 二叉树的右视图
给定一个二叉树的 根节点 root想象自己站在它的右侧按照从顶部到底部的顺序返回从右侧所能看到的节点值。
示例 1: 输入: [1,2,3,null,5,null,4]
输出: [1,3,4]示例 2:
输入: [1,null,3]
输出: [1,3]示例 3:
输入: []
输出: []提示:
二叉树的节点个数的范围是 [0,100]-100 Node.val 100
代码
class TreeNode:def __init__(self, x):self.val xself.left Noneself.right Noneclass Solution:def rightSideView(self, root: TreeNode) - list:if not root:return []res []curnode [root]nexnode []res.append(curnode[0].val)while curnode:for s in curnode:if s.right:nexnode.append(s.right)if s.left:nexnode.append(s.left)if nexnode:res.append(nexnode[0].val)curnode nexnodenexnode []return resdef listToTree(lst: list) - TreeNode:if not lst:return Noneroot TreeNode(lst[0])queue [root]i 1while i len(lst):node queue.pop(0)if lst[i] is not None:node.left TreeNode(lst[i])queue.append(node.left)i 1if i len(lst) and lst[i] is not None:node.right TreeNode(lst[i])queue.append(node.right)i 1return rootdef inorderTraversal(root: TreeNode) - list:if not root:return []res []res inorderTraversal(root.left)res.append(root.val)res inorderTraversal(root.right)return res# %%
s Solution()
null Nonenums [1,2,3,null,5,null,4]
root listToTree(nums)
print(s.rightSideView(root))
print(inorderTraversal(root)) #testnums [1,null,3]
root listToTree(nums)
print(s.rightSideView(root))
print(inorderTraversal(root)) #test输出
[1, 3, 4] [2, 5, 1, 3, 4] [1, 3] [1, 3] 3. 拼接最大数
给定长度分别为 m 和 n 的两个数组其元素由 0-9 构成表示两个自然数各位上的数字。现在从这两个数组中选出 k (k m n) 个数字拼接成一个新的数要求从同一个数组中取出的数字保持其在原数组中的相对顺序。
求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。
说明: 请尽可能地优化你算法的时间和空间复杂度。
示例 1:
输入:
nums1 [3, 4, 6, 5]
nums2 [9, 1, 2, 5, 8, 3]
k 5
输出:[9, 8, 6, 5, 3]
示例 2:
输入:
nums1 [6, 7]
nums2 [6, 0, 4]
k 5
输出:[6, 7, 6, 0, 4]
示例 3:
输入:
nums1 [3, 9]
nums2 [8, 9]
k 3
输出:[9, 8, 9]
代码
class Solution:def maxNumber(self, nums1: list, nums2: list, k: int) - list:def pick_max(nums, k):stack []drop len(nums) - kfor num in nums:while drop and stack and stack[-1] num:stack.pop()drop - 1stack.append(num)return stack[:k]def merge(A, B):lst []while A or B:bigger A if A B else Blst.append(bigger[0])bigger.pop(0)return lstreturn max(merge(pick_max(nums1, i), pick_max(nums2, k - i))for i in range(k 1)if i len(nums1) and k - i len(nums2))
# %%
s Solution()
print(s.maxNumber(nums1 [3,4,6,5], nums2 [9,1,2,5,8,3], k 5))
print(s.maxNumber(nums1 [6,7], nums2 [6,0,4], k 5))
print(s.maxNumber(nums1 [3,9], nums2 [8,9], k 3))
输出
[9, 8, 6, 5, 3] [6, 7, 6, 0, 4] [9, 8, 9]
注max(迭代推导式) -- max(i for i in [3,6,4,5] if i%2) 每日一练刷题专栏
✨ 持续努力奋斗做强刷题搬运工 点赞你的认可是我坚持的动力
★ 收藏你的青睐是我努力的方向
✏️ 评论你的意见是我进步的财富
C/C 每日一练 专栏 Python 每日一练 专栏 文章转载自: http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.ygxf.cn.gov.cn.ygxf.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.sbwr.cn.gov.cn.sbwr.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.pcqxr.cn.gov.cn.pcqxr.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn