반응형
보석 이름이 주어졌을 때
가지고 있는 돌 중에 보석인 것의 개수를 반환하는 문제.
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로 넘어간다.
반응형
'IT > 알고리즘' 카테고리의 다른 글
8] Leetcode 1313. Decompress Run-Length Encoded List (0) | 2020.06.21 |
---|---|
7] Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (0) | 2020.06.21 |
5] Leetcode 1342. Number of Steps to Reduce a Number to Zero (0) | 2020.06.21 |
4] Leetcode 1108. Defanging an IP Address (0) | 2020.06.21 |
3] Leetcode 1431. Kids With the Greatest Number of Candies (0) | 2020.06.18 |
댓글