풀이
class Solution {
public List<Integer> intersection(int[][] nums) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
ArrayList<Integer> answer = new ArrayList<>();
int k = nums.length;
for(int i=0;i<nums.length;i++) {
for(int j=0;j<nums[i].length;j++)
hashMap.put(nums[i][j], hashMap.getOrDefault(nums[i][j], 0)+1);
}
for(Integer key: hashMap.keySet()) {
if(k == hashMap.get(key))
answer.add(key);
}
Collections.sort(answer);
return answer;
}
}
다른 사람 풀이 for문
for (int[] arr: nums) {
for (int x: arr) {
counts.put(x, counts.getOrDefault(x, 0) + 1);
}
}
'Software Engineering > Algorithm' 카테고리의 다른 글
[LeetCode] #560 Subarray Sum Equals K (0) | 2024.07.06 |
---|---|
[LeetCode] #1941 Check if All Characters Have Equal Number of Occurrences (0) | 2024.06.23 |
[LeetCode] First Letter to Appear Twice (1) | 2024.06.22 |
[LeetCode] Two Sum (0) | 2024.06.22 |
[LeetCode] K Radius Subarray Averages (0) | 2024.06.10 |