| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 | 31 |
Tags
- loop
- dotenv
- var
- AI
- Python
- leetcode
- C#
- scss
- git
- C++
- TypeScript
- condition
- Clone
- VUE
- BOJ
- security
- vuetify
- webpack
- JavaScript
- nginx
- bash
- type
- docker
- vue.js
- property
- 보안
- 앙상블
- npm
- generic
- machine learning
Archives
- Today
- Total
ice rabbit programming
[LeetCode] May Challenge 2 - Jewels and Stones 본문
728x90
https://leetcode.com/problems/jewels-and-stones/
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
2일차 May Challenge 문제였는데, S에 속한 문자 중 J에 속한 것이 몇 개인지 구하는 문제였다. 단순한 선형 탐색을 통해 구현하였다. 다른 풀이로 string의 contains 메소드를 사용하였는데, 시간 차이는 4ms로 많이 나지 않았다. 아마 Contains 메소드가 O(nk)로 선형 탐색과 차이나지 않기 때문인 것 같다.
public class Solution {
public int NumJewelsInStones(string J, string S) {
int result=0;
for(int i=0;i<S.Length;i++) {
char temp=S[i];
for(int j=0;j<J.Length;j++) {
if(temp==J[j]) {
result++;
break;
}
}
}
return result;
}
}
public class Solution {
public int NumJewelsInStones(string J, string S) {
int result=0;
for(int i=0;i<S.Length;i++) {
char temp=S[i];
if(J.Contains(temp))
result++;
}
return result;
}
}
728x90
'PS > LeetCode' 카테고리의 다른 글
| [LeetCode] May Challenge 3 - Ransom Note (0) | 2020.05.04 |
|---|---|
| [LeetCode] Delete Duplicate Emails (0) | 2020.05.03 |
| [LeetCode] Second Highest Salary (0) | 2020.05.01 |
| [LeetCode] May Challenge 1 - First Bad Version (0) | 2020.05.01 |
| [LeetCode] Employees Earning More Than Their Manager (0) | 2020.05.01 |