프로그래머스 142086 가장 가까운 같은 글자

Jmnote (토론 | 기여)님의 2023년 11월 23일 (목) 01:15 판 (→‎C++)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

프로그래머스 142086 가장 가까운 같은 글자

2 C++[ | ]

#include <string>
#include <vector>
#include <map>
using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    map<char,int> m;
    for(int i=0; i<s.length(); i++) {
        char ch = s[i];
        map<char,int>::iterator it = m.find(ch);
        if(it == m.end()) {
            answer.push_back(-1);
        } else {
            answer.push_back(i-it->second);
        }
        m[ch] = i;
    }
    return answer;
}
#include <string>
#include <vector>
#include <map>
using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    map<char,int> m;
    for(int i=0; i<s.length(); i++) {
        char ch = s[i];
        if(m.find(ch) == m.end()) {
            answer.push_back(-1);
        } else {
            answer.push_back(i-m[ch]);
        }
        m[ch] = i;
    }
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}