给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。
例如,
给定二叉搜索树: 4 / \ 2 7 / \ 1 3 和 插入的值: 5
你可以返回这个二叉搜索树:
4 / \ 2 7 / \ / 1 3 5
或者这个树也是有效的:
5
/ \
2 7
/ \
1 3
\
4
class Solution { public: TreeNode *insertIntoBST(TreeNode *root, int val) { if (val > root->val && root->right) { insertIntoBST(root->right, val); } else if (val < root->val && root->left) { insertIntoBST(root->left, val); } else if (val > root->val && !root->right) { root->right = new TreeNode(val); } else { root->left = new TreeNode(val); } return root; } };
https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。
返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望索引的数字 i < j 且有 (time[i] + time[j]) % 60 == 0。
示例 1:
输入:[30,20,150,100,40] 输出:3 解释:这三对的总持续时间可被 60 整数: (time[0] = 30, time[2] = 150): 总持续时间 180 (time[1] = 20, time[3] = 100): 总持续时间 120 (time[1] = 20, time[4] = 40): 总持续时间 60
示例 2:
输入:[60,60,60] 输出:3 解释:所有三对的总持续时间都是 120,可以被 60 整数。
class Solution { public: int numPairsDivisibleBy60(vector<int> &time) { int num[60] = {0}, sum = 0; for (int n:time) { num[n % 60]++; } //60+60 sum += (num[0] - 1) * num[0] / 2; //30+30 sum += (num[30] - 1) * num[30] / 2; //(1-29)+(31-59) for (int x = 1; x <= 29; x++) { sum += num[x] * num[60 - x]; } return sum; } };
https://leetcode-cn.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/
我们有一个由平面上的点组成的列表 points。需要从中找出 K 个距离原点 (0, 0) 最近的点。
(这里,平面上两点之间的距离是欧几里德距离。)
你可以按任何顺序返回答案。除了点坐标的顺序之外,答案确保是唯一的。
示例 1:
输入:points = [[1,3],[-2,2]], K = 1 输出:[[-2,2]] 解释: (1, 3) 和原点之间的距离为 sqrt(10), (-2, 2) 和原点之间的距离为 sqrt(8), 由于 sqrt(8) < sqrt(10),(-2, 2) 离原点更近。 我们只需要距离原点最近的 K = 1 个点,所以答案就是 [[-2,2]]。
示例 2:
输入:points = [[3,3],[5,-1],[-2,4]], K = 2 输出:[[3,3],[-2,4]] (答案 [[-2,4],[3,3]] 也会被接受。)
class point { public: int x, y, d; point(int x, int y) { this->x = x; this->y = y; d = x * x + y * y; } }; bool cmp(point &p1, point &p2) { return p1.d < p2.d; } class Solution { public: static vector<vector<int>> kClosest(vector<vector<int>> &points, int K) { vector<point> p; p.reserve(points.size()); for (vector<int> P:points) { p.emplace_back(P[0], P[1]); } sort(p.begin(), p.end(), cmp); vector<vector<int>> ans; for (int x = 0; x < K; x++) { vector<int> tmp; tmp.push_back(p[x].x); tmp.push_back(p[x].y); ans.push_back(tmp); } return ans; } };
https://leetcode-cn.com/problems/k-closest-points-to-origin/
给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
- 输入的数组只包含
0
和1
。 - 输入数组的长度是正整数,且不超过 10,000。
class Solution { public: static int findMaxConsecutiveOnes(vector<int> &nums) { int max = 0, num = 0; for (int i : nums) { if (i == 1) num++; else num = 0; if (max < num) max = num; } return max; } };
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123 输出: 321
示例 2:
输入: -123 输出: -321
示例 3:
输入: 120 输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [-2^31, 2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
class Solution { public: int reverse(int x) { long long sum = 0, num = x; int flag = 1; if (num < 0) { flag = -1; num = -num; } while (num > 0) { sum = sum * 10 + num % 10; num /= 10; } if (sum > pow(2, 31)) { return 0; } return flag * sum; } };