반응형
    
    
    
  https://leetcode.com/problems/jewels-and-stones/
Jewels and Stones - LeetCode
Can you solve this real interview question? Jewels and Stones - You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to kno
leetcode.com
자료구조를 이용한 전수조사 문제였습니다.
📕 풀이방법
📔 입력 및 초기화
보석의 종류를 O(logn)으로 찾기 위해 map jewelsMap을 선언 후 jewels의 보석 종류를 key, value는 true로 저장해줍니다.
📔 풀이과정
stones의 원소를 순회하며 stones의 원소가 jewelsMap에 존재한다면 정답변수 ans에 누적해 더해줍니다.
📔 정답 출력 | 반환
ans를 반환합니다.
📕 Code
📔 C++
class Solution {
public:
    map <char, bool> jewelsMap;
    
    int numJewelsInStones(string jewels, string stones) {
        int ans = 0;
        jewelsMap.clear();
        for(auto j : jewels) jewelsMap[j] = true;
        for(auto s : stones) if(jewelsMap[s]) ans++;
        return ans;
    }
};*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > 자료구조' 카테고리의 다른 글
| (C++) - LeetCode (easy) 222. Count Complete Tree Nodes (0) | 2023.08.16 | 
|---|---|
| (C++) - LeetCode (easy) 884. Uncommon Words from Two Sentences (0) | 2023.08.11 | 
| (C++) - LeetCode (easy) 706. Design HashMap (0) | 2023.06.20 | 
| (C++) - LeetCode (easy) 703. Kth Largest Element in a Stream (0) | 2023.06.17 | 
| (C++) - LeetCode (easy) 697. Degree of an Array (0) | 2023.06.14 |