| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- BOJ
- machine learning
- C++
- docker
- type
- bash
- leetcode
- vuetify
- npm
- C#
- generic
- VUE
- nginx
- Python
- AI
- vue.js
- Clone
- scss
- TypeScript
- dotenv
- security
- webpack
- JavaScript
- condition
- 보안
- loop
- property
- git
- 앙상블
- var
Archives
- Today
- Total
ice rabbit programming
[LeetCode] May Challenge 5 - First Unique Character in a String 본문
728x90
4일차 문제를 푼 줄 알았는데 착오로 풀지 않고 넘어가버렸다 ㅠㅠ
https://leetcode.com/problems/first-unique-character-in-a-string/
First Unique Character in a String - 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
문자열에서 한 번만 등장하는 최초의 문자를 찾으면 된다. 문자 별 기본은 -1, 2회 이상 등장은 -2, 1회 등장은 index로 한 후에 최소값을 반환하였다.
class Solution {
public:
int firstUniqChar(string s) {
vector<int> counts(26,-1);
int result=2147483647;
for(int i=0;i<s.size();i++) {
if(counts[s[i]-'a']==-1)
counts[s[i]-'a']=i;
else
counts[s[i]-'a']=-2;
}
for(int i=0;i<26;i++) {
if(counts[i]>=0 && counts[i]<result)
result=counts[i];
}
if(result==2147483647)
return -1;
return result;
}
};728x90
'PS > LeetCode' 카테고리의 다른 글
| [LeetCode] May Challenge 9 - Single Element in a Sorted Array (0) | 2020.05.16 |
|---|---|
| [LeetCode] May Challenge 8 - Valid Perfect Square (0) | 2020.05.10 |
| [LeetCode] May Challenge 3 - Ransom Note (0) | 2020.05.04 |
| [LeetCode] Delete Duplicate Emails (0) | 2020.05.03 |
| [LeetCode] May Challenge 2 - Jewels and Stones (0) | 2020.05.03 |