2024-08-08

题目传送门:3131. 找出与数组相加的整数 I - 力扣(LeetCode)

签到题,排序后第一个相减即可。

最后实现代码如下:

1
2
3
4
5
6
7
8
class Solution {
public:
int addedInteger(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
return nums2[0] - nums1[0];
}
};

2024-08-09

题目传送门:3132. 找出与数组相加的整数 II - 力扣(LeetCode)

上一题的难度增强版,但是由于数组长度较小,直接枚举被删除的数即可

最后实现代码如下:

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
class Solution {
public:
int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {

sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int n = nums2.size();
function<int(int,int)> judge = [&](int ex1,int ex2){
int nw = 0;
while(nw==ex1||nw==ex2) nw++;
int x = nums2[0] - nums1[nw];
for(int i=0;i<n;++i){
while (i+nw==ex1||i+nw==ex2) nw++;
if(nums2[i]-nums1[nw+i]!=x) return 12312312;
}
return x;
};
int ans = INT_MAX;
for(int i=0;i<n+2;++i){
for(int j=i+1;j<n+2;++j){
int x = judge(i,j);
if (x!=12312312) ans = min(ans, x);
}
}
return ans;
}
};

本站由 @anonymity 使用 Stellar 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。