코딩공부/프로그래머스

[프로그래머스]Lv1 가장 가까운 같은 글자 C++

usingsystem 2023. 8. 11. 17:52
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/142086?language=cpp 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

소스코드

#include <string>
#include <vector>

using namespace std;

vector<int> solution(string s) {
	vector<int> answer;

	for (int i = 0;  i < s.length(); i++)
	{
		bool check = false;
		int count = 1;
		for (int j = i-1; j >= 0; j--)
		{
			if (s[i] == s[j])
			{
				answer.push_back(count);
				check = true;
				break;
			}
			count++;
		}
		if(check == false)
			answer.push_back(-1);
	}

	return answer;
}

 

728x90