반응형
Problem
이번 문제는 jewels 와 존 스톤스를 준다
존 스톤스 안에 jewels 가 몇개인지를 결과로 내줘야 하는 문제이다
나의 풀이
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
res = 0
for i in jewels:
res += stones.count(i)
return res
jewels 를 하나씩 돌려주면서 존스톤스 안에 몇개있는지 count를 사용하여 문제를 해결했다
다른 사람의 존스톤스 풀이를 보자
class Solution:
def numJewelsInStones (self, J: str, S: str) -> int:
jset = set(J)
count = 0
for i in S:
if i in jset:
count = count +1
return count
문제 풀이 방식은 다들 거의 다 같아서 패스하는대신 메모리사용량에서 차이가 있나해서 들다봣다
이분은 jewels에 set을 사용하여 중복값을 빼고 for loop을 덜돌릴수 있는 순두부 터치를 보여줬다..
반응형
'Python > algorithm' 카테고리의 다른 글
[Leetcode] 1603. Design Parking System (1) | 2022.12.30 |
---|---|
[Leetcode] 2114. Maximum Number of Words Found in Sentences (0) | 2022.12.29 |
[Leetcode] 1672. Richest Customer WealthEasy2.9K305 (0) | 2022.12.26 |
[Leetcode] 1512. Number of Good Pairs (0) | 2022.12.25 |
[Leetcode] 1470. Shuffle the Array (1) | 2022.12.21 |