글자 지우기

Subin·2024년 8월 8일

Algorithm

목록 보기
17/69

[내 풀이]

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, vector<int> indices) {
    string answer = "";

    for(int i : indices)
    {
        my_string[i] = 0;
    }

    for(auto c: my_string)
    {
        if(c != 0)
        {
            answer += c;
        }
    }

    return answer;
}

나는 아무 숫자나 공백을 덮어씌워서 그걸 나중에 빼고 넣는 방식으로 풀었지만

[다른 사람 풀이]

#include <bits/stdc++.h>
using namespace std;

string solution(string my_string, vector<int> indices) {
   sort(indices.begin(), indices.end(), greater<int>());
    for (int i : indices) {
        my_string.erase(i, 1);
    }
    return my_string;
}

erase 함수를 이용해서 푸는 방법도 있다.


참고 블로그 : https://velog.io/@doorbals_512/C-erase%EC%99%80-remove-%ED%95%A8%EC%88%98%EC%9D%98-%EC%B0%A8%EC%9D%B4
profile
성장하며 꿈꾸는 삶을 살아가고 있는 대학생입니다😊

0개의 댓글