Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 단기개발자코스
- infcon 2024
- TiL
- 전략패턴 #StrategyPattern #디자인패턴
- Spring multimodule
- 개발자부트캠프추천
- @FeignClient
- 커스텀 헤더
- 구글 OAuth login
- jwttoken
- 프로그래머스 이중우선순위큐
- 1주일회고
- 99클럽
- 빈 충돌
- jwt
- 디자인 패턴
- spring batch 5.0
- 파이썬
- 디자인패턴
- 인프콘 2024
- 코딩테스트 준비
- Python
- KPT회고
- 프로그래머스
- 개발자 취업
- 빈 조회 2개 이상
- 취업리부트코스
- 항해99
- DesignPattern
- JavaScript
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 771. Jewels and Stones with Python 본문
https://leetcode.com/problems/jewels-and-stones/
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
jewels_dic = {jewel: 0 for jewel in jewels}
for stone in stones:
if stone in jewels_dic:
jewels_dic[stone] += 1
return sum(jewels_dic.values())
stones에 jewels가 몇개 나왔는지 dictionary에 저장하고 더하는 식으로 풀었는데 아주 쿨한 코드를 발견했다.
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
return sum(s in jewels for s in stones)
이렇게도 풀 수 있구나를 알았다 ㄷㄷㄷ 굳!
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 347. Top K Frequent Elements with Python (0) | 2024.01.04 |
---|---|
LeetCode 3. Longest Substring Without Repeating Characters with Python (1) | 2024.01.04 |
LeetCode 739. Daily Temperatures with Python (1) | 2024.01.04 |
LeetCode 232. Implement Queue using Stacks with Python (0) | 2024.01.04 |
LeetCode 225. Implement Stack using Queues (0) | 2024.01.04 |