본문 바로가기
IT/알고리즘

6] Leetcode 771. Jewels and Stones

by 깻잎쌈 2020. 6. 21.
반응형
 

Jewels and Stones - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

보석 이름이 주어졌을 때 

가지고 있는 돌 중에 보석인 것의 개수를 반환하는 문제.

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int ans = 0;
        for(int i = 0;i<J.size();i++)
            for(int j = 0; j<S.size(); j++)
                if(J[i] == S[j]){
                    ans++;
                    continue;
                }
                    
         return ans;   
    }
};

보석의 배열과 돌의 배열을 모두 탐색하면서 

일치하는 게 있으면 다음 돌로 바로 넘어가야 하기 때문에 

 

이중 for문에서 S를 나중에 돌려주고

일치했을때 continue로 넘어간다.

반응형

댓글