一、题目
1.题目描述
给你一个二维整数数组 nums ,其中 nums[i] 是由 不同 正整数组成的一个非空数组,按 升序排列 返回一个数组,数组中的每个元素在 nums 所有数组 中都出现过。
2. 题目链接
二、解题报告
1.思路分析
- 由于nums[i]是由不同正整数组成,所以只要用哈希表存取nums[i]出现的元素,只要满足出现次数和nums的长度相同,即为相交数组。
2. 代码实现
class Solution {
public List<Integer> intersection(int[][] nums) {
List<Integer> res = new ArrayList<>();
int[] hash = new int[1001];
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums[i].length; j++){
hash[nums[i][j]]++;
}
}
for(int i = 0; i < hash.length; i++){
if(hash[i] == nums.length){
res.add(i);
}
}
return res;
}
}
3. 运行结果
三、总结
刚开始刷题时的手足无措,一个月的时间也足以在原来的基础上做出优化了。与诸君共勉,坚持终会有成果!