자연수 뒤집어 배열로 만들기(!)

김재혁·2025년 1월 23일
#include <string>
#include <vector>

using namespace std;

vector<int> solution(long long n) {
    vector<int> answer;
    string str = to_string(n);
    for(int i= str.size()-1;i>=0;--i)
    {
        answer.push_back(str[i]-'0');
    }
    
    return answer;
}
  • to_string = 숫자를 문자열로 바꿔주는 함수.
  • -'0'을 하는 이유 : 아스키 코드값으로 '3'-'0'을 하면 "51-48=3"이 되어서 3을 받아올 수 있음.

0개의 댓글