[프로그래머스] 이진 변환 반복하기

leejihun·2022년 11월 13일
0

알고리즘

목록 보기
38/50

https://school.programmers.co.kr/learn/courses/30/lessons/70129

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

vector<int> solution(string s) {
    vector<int> answer(2);// answer[0] : 몇번 턴이 돌았는지, answer[1]:제거된 0의 개수
    string temp;
    int length =0;
    while(1){
        if(s.size()==1) break;
        for(int i=0; i<s.size(); i++){
            if(s[i]=='0') { 
                answer[1]++;//제거된 0의 개수 증가시키기
                continue;
            }
            else length++;
        }
        s.clear();
        for(int i=length; i>0; i=i/2){
            s.push_back(i%2+'0');
        }
        length =0;

        answer[0]++; //한 턴이 돌았다는 의미
    }
    return answer;
}

for문 돌리면서 Num.erase(Num.begin() + i);
iZeroNum++;

직접 지워가면서 string을 계속 새로만들고 하다보니 메모리 문제가 발생했었다.

profile
U+221E

0개의 댓글