Weakly Contest 169
题目
题目一
5295. Find N Unique Integers Sum up to Zero
Given an integer n, return any array containing n unique integers such that they add up to 0.
Example 1:
1 | |
Example 2:
1 | |
解题报告
理解题意
- 这是一道简单题
- 给定一个数,求数组
- 要求数组的所有元素和位0
思路
- 给的数有可能为奇数、也有可能为偶数
- 如果是奇数:从0开始向左右拓展,最后加上0即可
- 如果为偶数:从0开始向左右拓展
代码
1 | |
题目二
5296. All Elements in Two Binary Search Trees My SubmissionsBack to Contest
Given two binary search trees root1 and root2.
Return a list containing all the integers from both trees sorted in ascending order.
Example 1:
1 | |
解题报告
理解题意
- 这是一道中档难度题
- 给定两个二叉排序树,要求返回一个包含所有元素并且是递增顺序的列表
思路
- 给定的两个二叉搜索树有可能为空
- 二叉搜索树中序遍历即为递增排序
- 遍历两个二叉搜索树,然后对归并排序,时间复杂度为 \(\mathcal{O(max(m,n))}\)
代码
1 | |
Weakly Contest 169
https://bapuqln-blog.pages.dev/2019/12/29/Weakly-Contest-169/