第283场周赛

6016. Excel 表中某个范围内的单元格 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<string> cellsInRange(string s) {
char s1 = s[0], s2 = s[3];
char n1 = s[1], n2 = s[4];
int n = s2 - s1, m = n2 - n1;
vector<string> res;
for (int i = 0; i <= n; i ++) {
char t = n1;
for (int j = 0; j <= m; j ++) {
string s = "";
s.append(1, s1);
s.append(1, t);
t ++;
res.push_back(s);
}
s1 ++;
}
return res;
}
};

6017. 向数组中追加 K 个整数 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
long long minimalKSum(vector<int>& nums, int k) {
nums.push_back(0);
nums.push_back(2e9);
sort(nums.begin(), nums.end());
long long res = 0;
for (int i = 1; i < nums.size(); i ++) {
int fill = nums[i] - nums[i - 1] - 1; // 要填充的数
if (fill < 0) continue;
if (fill >= k) {
res += (long long) (nums[i - 1] + 1 + nums[i - 1] + k) * k / 2;
break;
}
res += (long long) (nums[i - 1] + nums[i]) * fill / 2;
k -= fill;
}
return res;
}
};

6018. 根据描述创建二叉树 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
unordered_map<int, TreeNode*> map;
set<int> childs;
for (auto vec: descriptions) {
int pVal = vec[0], cVal = vec[1];
bool juge = vec[2] == 1 ? true : false;

auto pNode = map[pVal];
if (pNode == nullptr) {
pNode = new TreeNode(pVal);
map[pVal] = pNode;
}

auto cNode = map[cVal];
if (cNode == nullptr) {
cNode = new TreeNode(cVal);
map[cVal] = cNode;
}
childs.insert(cVal);

if (juge) {
pNode->left = cNode;
} else {
pNode->right = cNode;
}
}
// for (auto i: childs) cout << i << endl;
TreeNode* root = nullptr;
for (auto vec: descriptions) {
if (childs.count(vec[0])) continue;
else {
root = map[vec[0]];
break;
}
}
return root;
}
};

6019. 替换数组中的非互质数 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<int> replaceNonCoprimes(vector<int>& nums) {
vector<int> vec{nums[0]};
for (int i = 1; i < nums.size(); i ++) {
vec.push_back(nums[i]);
while (vec.size() > 1) {
int x = vec.back(), y = vec[vec.size() - 2];
int t = gcd(x, y);
if (t == 1) break;
vec.pop_back();
vec.back() *= x / t;
}
}
return vec;
}

int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
};

第283场周赛
https://2w1nd.github.io/2022/03/06/LC周赛/第283场周赛/
作者
w1nd
发布于
2022年3月6日
许可协议