문자열 뒤집기

Subin·2024년 7월 25일

Algorithm

목록 보기
10/69

[내 풀이]

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, vector<vector<int>> queries) {
    string answer = "";
    string new_string = my_string; // 새로운 문자열 저장할 곳 생성

    for(auto q: queries)
    {//my_string의 q[0]번째부터 q[1]번째까지 반대로
        for(int i=q[0]; i<= q[1]; i++)
        {
            int j = q[1]-i+q[0]; //j번째는 q[1]부터 q[0]까지 거꾸로 (-1)
            new_string[i] = my_string[j];

        }
        // 한 번 바꾸고 그 다음 순서엔 새로운 문자열로 기존 문자열 업데이트
        my_string = new_string;
    }
    answer = new_string;
    return answer;
}

[다른 사람 풀이]

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string my_string, vector<vector<int>> queries) {
    for(const auto& q : queries)
    {
        reverse(my_string.begin()+q[0],my_string.begin()+q[1]+1);
    }
    return my_string;
}

뒤집는 건 reverse 함수 이용하기!
reverse(first, last) : first포함 ~ last바로 전 지점 까지 뒤집음

profile
성장하며 꿈꾸는 삶을 살아가고 있는 대학생입니다😊

0개의 댓글