반응형
https://leetcode.com/problems/set-mismatch/description/
Set Mismatch - LeetCode
Can you solve this real interview question? Set Mismatch - You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which res
leetcode.com
구현문제였습니다.
📕 풀이방법
📔 입력 및 초기화
1. nums의 원소와 그 빈도수를 key, value로 저장할 map 변수 numsMap을 선언해 값을 저장해줍니다.2. 정답 변수 ans를 선언해 줍니다.
📔 풀이과정
1. 겹친 수를 ans에 push_back해줍니다.
2. 누락된 수를 마찬가지로 넣어줍니다.
📔 정답 출력 | 반환
ans를 반환합니다.
📕 Code
📔 C++
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
map<int,int> numsMap;
for(auto n : nums) {
numsMap[n]++;
}
vector<int> ans;
for(int i = 1; i <= nums.size(); i++){
if(numsMap[i] > 1) ans.push_back(i);
}
for(int i = 1; i <= nums.size(); i++){
if(!numsMap[i]) ans.push_back(i);
}
return ans;
}
};
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > Implementation' 카테고리의 다른 글
(C++) - LeetCode (easy) 661. Image Smoother (0) | 2023.06.05 |
---|---|
(C++) - LeetCode (easy) 657. Robot Return to Origin (0) | 2023.06.02 |
(SQL) - LeetCode (easy) 610. Triangle Judgement (0) | 2023.05.20 |
(C++) - LeetCode (easy) 605. Can Place Flowers (0) | 2023.05.17 |
(C++) - LeetCode (easy) 566. Reshape the Matrix (0) | 2023.04.25 |