第一周作业
目录:
- 本周完成题目
- 主要过程思路
- 相关代码
- 感想与总结
一、本周完成题目
本周共完成六道题目,其中1道Easy,4道Medium,1道Hard。由于基本都为Array分类中的题目且题目间有相似性,相对而言比较简单。 leetcode
具体完成题目及难度如下表:
# | Title | Difficulty |
---|---|---|
1 | Two Sum | Easy |
4 | Median of Two Sorted Arrays | Hard |
15 | 3Sum | Medium |
16 | 3Sum Closest | Medium |
18 | 4Sum | Medium |
454 | 4Sum II | Medium |
题目内容
1、Sum类型
主要内容为寻找数组中和为Target的组合,包括:
Two Sum、3Sum、3Sum Closest、4Sum四道相似的问题,之后会以其中的3Sum为例对于类似问题进行分析。
2、4Sum II
给定4个长度为N(≤500)的数组,从每个数组中任取一个数字使4个数字和为0,求组合数量。
Input:
A = [ 1, 2] B = [-2,-1]
C = [-1, 2] D = [ 0, 2]
Output:
2
Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
3、Median of Two Sorted Arrays
求两个排序后的数组的中位数。
二、主要过程思路
3Sum
本题在刚开始时遇到了一些问题,主要是对于细节的理解有所欠缺,比如对于重复出现数字的处理、空数组的处理。最大的问题在于算法超时的问题。
最初的算法中,使用了排序后3次循环比较的算法,虽然做了一定的优化但是在数字较多的情况下会超时。
后来在参考了一些别人的算法后采用了下面的方式进行处理。
对于每个数字,首先排除与前一项的重复问题,之后在该数字后面寻找相加为该数字相反数的两个数字。具体寻找方法为,设定l、r两个数字,从两端开始逐步缩小范围(循环运行直到l≥r或找到合适项),再找到合适的数字后push到数组中。
4Sum II
这一题与上面的方法有所不同。由于4个数字需要来自不同的数组,且和为0,可以将数组分为两组,A与B为一组,C与D为一组。为了简化算法,使用map进行存储,将A、B中所有的相加组合存入map中并以和为key,出现次数为值。
再次循环,在C与D两两相加时,寻找map中是否有相反数,如有则加上其出现次数。
Median of Two Sorted Arrays
由于数组已经排序,所以需要从两个数字起始处开始,按照大小进行遍历,使两个数组共遍历k个数字。k=(m+n)/2。如m+n为偶数则需要额外再寻找一个数字。
方法比较简单,类似指针移动,只需要设置两个位置编号ai、bi和一个计数编号i,判断两个数组当前位置(根据ai、bi)的数字大小,选择较小的那个作为下一个数字同时更新标号即可。
三、相关代码
3Sum
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
int length = nums.size();
if (length < 3)
return result;
sort(nums.begin(), nums.end());
for (int j = 0; j <= length - 3; j++) {
if (j != 0 && nums[j] == nums[j - 1]) continue;
if(nums[j]+nums[length-2]+nums[length-1]<0) continue;
if ((nums[j] + nums[j + 1] + nums[j + 2]) > 0) break;
int l = j + 1;
int r = length - 1;
int target2 =- nums[j];
while (l < r) {
int temp = nums[l] + nums[r];
if (temp == target2) {
result.push_back(vector<int> { nums[j],nums[l], nums[r] });
while (l + 1 < r && nums[l] == nums[l + 1]) l++;
while (r - 1 > l && nums[r] == nums[r - 1])r--;
l++;r--;
} else if (temp < target2) {
l++;
} else {
r--;
}
}
}
return result;
}
};
4Sum II
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int count=0;
int N=A.size();
map<int,int> ab,cd;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
ab[A[i]+B[j]]++;
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
count+=ab[-(C[i]+D[j])];
}
}
return count;
}
};
Median of Two Sorted Arrays
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
double res, res2;
int m = nums1.size(), n = nums2.size();
double k = ((double) m + (double) n) / 2, k1 = 0;
if ((m + n) % 2 == 1) {
k1 = 1;
}
int i = 0, ai = 0, bi = 0;
while (i < k) {
if (ai == m) {
res = nums2[bi];
bi++;
} else if (bi == n) {
res = nums1[ai];
ai++;
} else {
nums1[ai] > nums2[bi] ? res = nums2[bi] : res = nums1[ai];
nums1[ai] > nums2[bi] ? bi++ : ai++;
}
i++;
}
if (k1 == 1) {
return res;
}
if (ai == m) {
res2 = nums2[bi];
} else if (bi == n) {
res2 = nums1[ai];
} else {
nums1[ai] > nums2[bi] ? res2 = nums2[bi] : res2 = nums1[ai];
}
return (res + res2) / 2;
}
};
四、感想与总结
本次为第一次作业,由于一段时间没有进行在线编程,所以先选择了一些数组方面的题目。整体来说比较简单,但是也需要进行算法上的设计与思考才能符合时间标准。
实现的代码还不够简洁快速,需要进一步进行优化。